I am getting an error : rest_framework.request.Wra

2019-04-06 15:26发布

问题:

urls.py

from django.conf.urls import url
from django.contrib import admin 
from django.conf import settings 
from django.conf.urls.static import static 
from .views import home 
from posts.views import PostListView 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls),
    url(r'^$', PostListView.as_view(), name='home'), 
    url(r'^post/', include('posts.urls', namespace='post')), 
    url(r'^api/post/', include('posts.api.urls', namespace='post-api')), 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

api/views.py

from rest_framework import generics

from posts.models import Post
from .serializers import PostModelSerializer

class PostListAPIView(generics.ListAPIView):
    serializer_class = PostModelSerializer

    def get_queryset(self):
        return Post.objects.all()

api/serializers.py

from rest_framework import serializers
from posts.models import Post

class PostModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        field = [
            'user',
            'content'
        ]

api/urls.py

from django.conf.urls import url
# from django.contrib import admin

# from django.conf import settings
# from django.conf.urls.static import static
# from .views import home

from .views import PostListAPIView
from django.views.generic.base import RedirectView


urlpatterns = [
    # url(r'^(?P<pk>\d+)/update/$', PostUpdateView.as_view(), name='update'),
    # url(r'^(?P<pk>\d+)/delete/$', PostDeleteView.as_view(), name='delete'),
    # url(r'^(?P<pk>\d+)/$', PostDetailView.as_view(), name='detail'),
    url(r'^$', PostListAPIView.as_view(), name='list'), #/api/tweet
    # url(r'^$', RedirectView.as_view(url='/')),
    # url(r'^create/$', PostCreateView.as_view(), name='create'),
]

views.py

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

from django.conf import settings
from django.conf.urls.static import static
from .views import home
from posts.views import PostListView


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', PostListView.as_view(), name='home'),
    url(r'^post/', include('posts.urls',  namespace='post')),
    url(r'^api/post/', include('posts.api.urls',  namespace='post-api')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

enter image description here

回答1:

The docs state that Django Rest Framework works with Django versions 1.11, 2.0, 2.1, although the process_request() method of CSRFCheck was actually introduced in Django 1.11.6

The version of Django you're using (1.10.3) is too old for your version of Django Rest Framework.

You should upgrade Django to version 1.11.6 or ideally higher.

pip install --upgrade django  # Upgrade to the latest Django version

or

pip install --upgrade django==1.11.6  # Upgrade to version 1.11.6