Ale*_*y K 6 django django-rest-framework
我尝试创建一个视图,它将接受POST请求并创建我的模型的新实例(参见帖子的底部).我按照本教程.问题是,当我访问与视图相关联的URL,从CreateAPIView继承我没有看到在API的HTML表示形式创造新的实例,我也看到,它接受GET请求,没有职位,因为它在文档中提到的.
页面看起来像这样
我的views.py
from django.shortcuts import render
from rest_framework.generics import ListAPIView, CreateAPIView
from datingapp.models import Profile
from .serializers import ProfileSerializer, ProfileCreateSerializer
class ProfilesAPIView(ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
class ProfileCreateAPIView(CreateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileCreateSerializer
Run Code Online (Sandbox Code Playgroud)
我的urls.py
from django.conf.urls import url
from django.contrib import admin
from datingapp.views import ProfilesAPIView, ProfileCreateAPIView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
]
Run Code Online (Sandbox Code Playgroud)
我的serializers.py
from rest_framework.serializers import ModelSerializer
from datingapp.models import Profile
class ProfileSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
Run Code Online (Sandbox Code Playgroud)
在我的settings.py中,我安装了crispy_forms.
我究竟做错了什么 ?
UPD:这就是我想要实现的目标
如您所见,有一个表单,它只接受POST,并且还说不允许GET
Håk*_*Lid 16
问题出在你的路由器上.第一个模式匹配两者api/profiles/,api/profiles/create/因此永远不会评估第二个模式.您正在查看ProfilesAPIView而不是创建视图.
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
Run Code Online (Sandbox Code Playgroud)
要修复它,要么交换网址的顺序,要么添加$到第一个模式的末尾.r'api/profiles/$'
我正在学习教程并遇到了类似的问题。可能我没有遵循相同版本的 Django Rest Framework,他们进行了更改。但是我这样做解决了这个问题。
class AssetBundleList(generics.ListAPIView):
Run Code Online (Sandbox Code Playgroud)
到
class AssetBundleList(generics.ListCreateAPIView):
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助某人。
| 归档时间: |
|
| 查看次数: |
10474 次 |
| 最近记录: |