如何在django中获取POST数据1.3 [英] how to get POST data in django 1.3

查看:414
本文介绍了如何在django中获取POST数据1.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在关注本教程,学习用Django制作维基页面。然而,它是在django 0.96,我使用Django 1.3,所以有一些事情是不同的。有些我已经修改了自己,但是这个我似乎无法使其工作。

Hey, I am following this tutorial to learn to make a wiki page with Django. However, it is made in django 0.96 and I use Django 1.3 so there are some things that are different. Some I already fixed myself, however this one I can't seem to make it work.

我提交了一个表单,将数据提交给一个视图。
这是以下形式:

I made a form that submits data to a view. This is the form:

<form method="post" action"/wikicamp/{{page_name}}/save/">{% csrf_token %}
    <textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
    <input type="submit" value="Save Page"/>
</form>

和/ wikicamp / {{page_name}} / save / url重定向到save_page视图: / p>

and the /wikicamp/{{page_name}}/save/ url redirects to the save_page view:

from django.http import HttpResponseRedirect
from django.core.context_processors import csrf

def save_page(request, page_name):
    c = {}
    c.update(csrf(request))
    content = c.POST["content"]
    try:
        page = Page.objects.get(pk=page_name)
        page.content = content
    except Page.DoesNotExist:
        page = Page(name=page_name, content=content)
    page.save()
    return HttpResponseRedirect("wikicamp/" + page_name + "/")

但是问题是我收到这个错误:

However the problem is that I get this error:

Help

Reason given for failure:

    CSRF token missing or incorrect.


In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

    The view function uses RequestContext for the template, instead of Context.
    In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
    If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.

You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.

You can customize this page using the CSRF_FAILURE_VIEW setting.

所以我阅读了一些文档,如 http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#如何使用的,它的。我试图做到这一点,但仍然给出了相同的错误。

So I read through some of the documentation, like http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it. I tried to do that however and it still gave the same error.

所以:任何人都有一个想法,如何处理表单数据与Django 1.3? strong>

So: Anyone an idea how to handle form post data well with Django 1.3?

我认为它有一些关系:view函数使用RequestContext作为模板,而不是Context。但我现在不知道是什么。

I think it has something to do with: The view function uses RequestContext for the template, instead of Context. but i don't now what it is.

btw,在我的终端中显示本地主机的http请求,它说:A {%csrf_token%}被使用在模板中,但上下文没有提供值。这通常是由于不使用RequestContext引起的。

btw, in my terminal which shows the http request of the localhost it says this: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.

推荐答案

您必须包含 {%csrf_token%} 您的< form> 标签之间的表格模板。

You've got to include {% csrf_token %} in your form's template between your <form> tags.

<form method="post" action"/wikicamp/{{page_name}}/save/">
    {% csrf_token %}
    <textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
    <input type="submit" value="Save Page"/>
</form>

如果 csrf_token 没有呈现到表单确保您在视图的响应中提供 RequestContext

If the csrf_token is not rendered into your form make sure you're providing the RequestContext in the view's response:

from django.shortcuts import render_to_response
from django.template import RequestContext

def app_view(request):
    return render_to_response('app_template.html', 
                              app_data_dictionary, 
                              context_instance=RequestContext(request))

或使用此快捷方式:


Or, use this shortcut method:

from django.views.generic.simple import direct_to_template

def app_view(request):             
    return direct_to_template(request, 'app_template.html', app_data_dictionary)

RequestContext 在使用通用视图时始终可用。

The RequestContext is always available when you're using generic views.

这篇关于如何在django中获取POST数据1.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆