Python 使用Django Restframework定制Rest的好例子
在过去的两天里,我一直在尝试使用djangorestframework编写一个自定义rest接口——Get和Post。获得没有任何问题的作品 然而,这个职位不起作用 以下是我所做的: 编写了一个forms.py文件,其中包含以下内容Python 使用Django Restframework定制Rest的好例子,python,django,django-rest-framework,Python,Django,Django Rest Framework,在过去的两天里,我一直在尝试使用djangorestframework编写一个自定义rest接口——Get和Post。获得没有任何问题的作品 然而,这个职位不起作用 以下是我所做的: 编写了一个forms.py文件,其中包含以下内容 import logging from django import forms class AddUserLocationAndDetailsForm(forms.Form): """A simple form with some of the most i
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)
在运行服务器时执行此操作后,我将获得输入两个字段的网页。但每当我提交帖子时,我都会得到一个错误:“这个字段是必需的。”。我不知道发生了什么事。不知道如何调试它,因为它从来没有在类AddUserLocationAndDetailsView中点击“post”函数。
我在示例中介绍了他们的做法:
但它根本不起作用。非常感谢您的帮助。我希望您能:
from <yourapp>.forms import AddUserLocationAndDetailsForm
不确定这是否有帮助。。。。因为这样会在runserver上出现错误或点击页面
您的设置文件中是否启用了调试模式
你能给你的URL文件吗
from forms import AddUserLocationAndDetailsForm