TypeError:at /' module'对象不可调用

时间:2015-07-20 10:02:39

标签: python django django-rest-framework

models.py:

import datetime
from django.db import models
from pygments.lexers import get_all_lexers

LEXERS = [item for item in get_all_lexers() if item[1]]

class Classname(models.Model):
    class_name = models.CharField(max_length=8)
    def __str__(self):
        return self.class_name

class Sectionname(models.Model):
    class_name = models.ForeignKey(Classname)
    section_name = models.CharField(max_length=1, default='A')
    def __str__(self):
        return self.section_name

class Teachername(models.Model):
    classname = models.ForeignKey(Classname, verbose_name='class Name')
    secname = models.ForeignKey(Sectionname, verbose_name='sectionname')
    teachname = models.CharField(max_length=50, verbose_name='teacher Name')
    def __str__(self):
        return self.teachname

class Attendancename(models.Model):
    teacher_name = models.ForeignKey(Teachername)
    date = models.DateField('Date')
    intime = models.TimeField('IN-TIME')
    outtime = models.TimeField('OUT-TIME')

    def hours_conversion(self):
        tdelta = (datetime.datetime.combine(datetime.date.today(),self.outtime) - datetime.datetime.combine(datetime.date.today(),self.intime))
    hours, minutes = tdelta.seconds//3600, (tdelta.seconds//60)%60
        return '{0}hrs {1}mins'.format(hours, minutes)

    def __str__(self):
         return "%s" %self.teacher_name

views.py:

from django.contrib.auth.models import User
from .models import Classname, Sectionname, Teachername, Attendancename
from rest_framework import generics, reverse
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .serializers import ClassSerializer, SectionSerializer, TeacherSerializer, AttendanceSerializer, UserSerializer
from rest_framework.permissions import IsAuthenticatedOrReadOnly



#serializing code starts from here!
@api_view(('GET',))    """when I add this view it raises above exception"""
def api_root(request, format=None):
    return Response({
    'users': reverse('user-list', request=request, format=format),
    'class':reverse('class', request=request, format=format),
    'section':reverse('student:section', request=request, format=format),
    'teacher':reverse('student:teacher', request=request, format=format),
    'attendance':reverse('student:attend', request=request, format=format)
    })

class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class ClassList(generics.ListCreateAPIView):
    queryset = Classname.objects.all()
    serializer_class = ClassSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class ClassDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Classname.objects.all()
    serializer_class = ClassSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class SectionList(generics.ListCreateAPIView):
    queryset = Sectionname.objects.all()
    serializer_class = SectionSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class SectionDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Sectionname.objects.all()
    serializer_class = SectionSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class TeacherList(generics.ListCreateAPIView):
    queryset = Teachername.objects.all()
    serializer_class = TeacherSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class TeacherDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Teachername.objects.all()
    serializer_class = TeacherSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class AttendanceList(generics.ListCreateAPIView):
    queryset = Attendancename.objects.all()
    serializer_class = AttendanceSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class AttendanceDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Attendancename.objects.all()
    serializer_class = AttendanceSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

urls.py:

from django.conf.urls import url, patterns
from . import views

urlpatterns = [
    #DRF urls start from here...
    url(r'^', views.api_root),
    url(r'^users/$', views.UserList.as_view()),
    url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
    url(r'^class/$', views.ClassList.as_view(), name='class'),
    url(r'^class/(?P<pk>[0-9]+)/$', views.ClassDetail.as_view(), name='classdet'),
    url(r'^section/$', views.SectionList.as_view(), name='section'),
    url(r'^section/(?P<pk>[0-9]+)/$', views.SectionDetail.as_view(), name='sectiondet'),
    url(r'^teach/$', views.TeacherList.as_view(), name='teacher'),
    url(r'^teach/(?P<pk>[0-9]+)/$', views.TeacherDetail.as_view(), name='teacherdet'),
    url(r'^attend/$', views.AttendanceList.as_view(), name='attend'),
    url(r'^attend/(?P<pk>[0-9]+)/$', views.AttendanceDetail.as_view(), name='attendet'),
 ]

这是我的完整追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/stu/class/

Django Version: 1.8.3
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'student')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\User\demo\venv\lib\site-    packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\django\views\generic\base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\views.py" in dispatch
  456.             response = self.handle_exception(exc)
 File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\views.py" in dispatch
  453.             response = handler(request, *args, **kwargs)
 File "C:\Users\User\demo\venv\lib\site-packages\rest_framework\decorators.py" in handler
  50.             return func(*args, **kwargs)
File "C:\Users\User\demo\p\oursite\student\views.py" in api_root
  21.       'users': reverse('user-list', request=request, format=format),

Exception Type: TypeError at /stu/class/
Exception Value: 'module' object is not callable

我正在关注DRF教程并且我在我自己的演示应用程序上尝试它,除了尝试在我的views.py文件中添加'root_api'视图时,一切正常提出异常。

我想在教程中为我的所有观点创建一个入口点。

我不知道为什么它会显示这种行为,因为我也在我的网址中使用了命名空间。我在某个地方做错了吗?

请!帮我修复它....

谢谢!提前.....

1 个答案:

答案 0 :(得分:6)

你错过了进口的东西。它不是

from rest_framework import reverse

from rest_framework.reverse import reverse