网址配置不正确

时间:2017-08-03 17:51:50

标签: django django-rest-framework

我正在尝试使用Django REST Framework。我使用的是Django 1.10和DRF 3.6.2。

我错过了什么,有人可以告诉我!!!

我在示例django项目中创建了一个“api”应用程序。在我的serializers.py

from django.contrib.auth.models import User
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ['url', 'username', 'email',]

在我的views.py

from django.contrib.auth.models import User
from rest_framework import generics
from .serializers import UserSerializer


class UserListAPIView(generics.ListAPIView):

    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer

在我的项目中urls.py

from django.conf.urls import url, include
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('api.urls', namespace='users')),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),

]

在我的应用“api”urls.py

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


urlpatterns = [
    url(r'^api/users', views.UserListAPIView.as_view(), name='user-list'),
]

看起来一切都很好。但是当我拨打http://127.0.0.1:8000/api/users/

显示---

enter image description here

1 个答案:

答案 0 :(得分:1)

删除' url'它从字段尝试向用户添加详细信息视图,但您还没有:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ['username', 'email',]
        #        ^^^^^^^^^^^^^^^^^^^^^

或尝试使用readonlymodelviewset