0

过去两天我一直在尝试编写一个自定义的休息界面——使用 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

但这根本行不通。任何帮助表示赞赏。

4

1 回答 1

0

我希望进口:

from <yourapp>.forms import AddUserLocationAndDetailsForm

代替:

from forms import AddUserLocationAndDetailsForm

不确定这是否有帮助....因为这会在运行服务器上出错或点击页面您的设置文件中是否启用了调试模式?

你也可以给你的urls文件吗?

于 2012-07-11T23:02:13.740 回答