使用参数'()'和关键字参数'{}'找不到'userlist'的反转。尝试过0种模式:[]

时间:2015-07-15 06:55:56

标签: python django django-rest-framework

views.py:

from .models import Classname
from .serializers import ClassSerializer, UserSerializer
from rest_framework import generics, permissions, renderers
from rest_framework.decorators import api_view
from .permissions import IsOwnerOrReadOnly
from rest_framework.response import Response
from django.contrib.auth.models import User
from rest_framework.reverse import reverse


@api_view(('GET',))
def api_root(request, format=None):
    return Response({
        'users': reverse('userlist', request=request, format=format),
        'fab': reverse('classlist', request=request, format=format)
    })
class ClassList(generics.ListCreateAPIView):
    queryset = Classname.objects.all()
    serializer_class = ClassSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

class ClassDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

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

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

class ClassHighlight(generics.GenericAPIView):
    queryset = Classname.objects.all()
    renderer_classes = (renderers.StaticHTMLRenderer,)

def get(self, request, *args, **kwargs):
    fab = self.get_object()
    return Response(fab.highlighted)

urls.py:

from django.conf.urls import url
from rest_framework import renderers
from . import views

urlpatterns = [
    url(r'^$', views.api_root),
    url(r'^classlist/$', views.ClassList.as_view(), name = 'classlist'),
    url(r'^class/(?P<pk>[0-9]+)/highlight/$', views.ClassHighlight.as_view()),
    url(r'^classdetail/(?P<pk>[0-9]+)/$', views.ClassDetail.as_view(), name = 'classdetail'),
    url(r'^userlist/$', views.UserList.as_view(), name='userlist'),
    url(r'^userdetail/$', views.UserDetail.as_view(), name='userdetail'),
]

这是我的完整追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/how/

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',
 'fab')
 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\dem\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\dem\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
58.         return view_func(*args, **kwargs)
File "C:\Users\User\dem\venv\lib\site-packages\django\views\generic\base.py" in view
71.             return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\dem\venv\lib\site-packages\rest_framework\views.py" in dispatch
456.             response = self.handle_exception(exc)
File "C:\Users\User\dem\venv\lib\site-packages\rest_framework\views.py" in dispatch
453.             response = handler(request, *args, **kwargs)
File "C:\Users\User\dem\venv\lib\site-packages\rest_framework\decorators.py" in handler
50.             return func(*args, **kwargs)
File "C:\Users\User\dem\try\howsthat\fab\views.py" in api_root
16.         'users': reverse('userlist', request=request, format=format),
File "C:\Users\User\dem\venv\lib\site-packages\rest_framework\reverse.py" in reverse
26.     return _reverse(viewname, args, kwargs, request, format, **extra)
File "C:\Users\User\dem\venv\lib\site-packages\rest_framework\reverse.py" in _reverse
37.     url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "C:\Users\User\dem\venv\lib\site-packages\django\core\urlresolvers.py" in reverse
579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Users\User\dem\venv\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
496.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /how/
Exception Value: Reverse for 'userlist' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

当我尝试在我的views.py文件中添加'api_root'视图时,它显示上面的错误。因为我在urls.py中也使用了命名空间,我不知道它为什么会抛出这个错误?

请帮我解决....

谢谢!提前......

1 个答案:

答案 0 :(得分:5)

尝试包含您在添加urls.py时使用的命名空间。

reverse('how:userlist', request=request, format=format),