使用Django Rest Framework定制Rest的好例子

时间:2012-05-19 20:32:05

标签: python django django-rest-framework

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

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

1 个答案:

答案 0 :(得分:0)

我希望导入:

from <yourapp>.forms import AddUserLocationAndDetailsForm

代替:

from forms import AddUserLocationAndDetailsForm

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

你能给你的网址文件吗?