过去两天我一直在尝试编写一个自定义的休息界面——使用 djangorestframework 获取和发布。获得没有任何问题的作品。
但是 POST 不起作用。
以下是我所做的:用以下内容编写了一个 forms.py 文件
import logging
from django import forms
class AddUserLocationAndDetailsForm(forms.Form):
"""A simple form with some of the most important pygments settings.
The code to be highlighted can be specified either in a text field, or by URL.
We do some additional form validation to ensure clients see helpful error responses."""
user = forms.CharField(
label='User',
max_length=200)
fbemail = forms.CharField(
label='Facebook Email',
max_length=200)
像这样写了相应的view.py文件:
import logging
from djangorestframework.views import View
from djangorestframework.permissions import IsAuthenticated
from django.core.urlresolvers import reverse
from djangorestframework.response import Response
from djangorestframework import status
from forms import AddUserLocationAndDetailsForm
class AddUserLocationAndDetailsView(View):
"""
This view adds details of a user.
This view also performs functionalities to update location of a user if the user already exists.
"""
form = AddUserLocationAndDetailsForm
def get(self, request):
return "you have reached get"
def post(self, request):
return Response(status.HTTP_201_CREATED)
在我运行服务器时执行此操作后,我会获得用于输入两个字段的网页。但是每当我提交 POST 时,我都会收到错误“此字段是必需的。”。我不知道发生了什么。也不知道如何调试它,因为它从不点击 AddUserLocationAndDetailsView 类中的“post”函数。我已经按照他们在这里的例子做了:http: //django-rest-framework.org/examples/views.html
但这根本行不通。任何帮助表示赞赏。