csrf_token的问题 [英] problems with csrf_token

查看:115
本文介绍了csrf_token的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django教程中做wiki,在教程中,早期版本的django被用于csrf token不强制的地方。我已经在表单中添加了{%csrf_token%},但我不知道在视图中该怎么做。请帮助。

I'm doing wiki in django tutorial and in tutorial earlier version of django is used where csrf token is not compulsory. I've added {% csrf_token %} to the form, however I'm not sure what to do in the views. Please help.

以下形式:

<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>

views.py:

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
    except Page.DoesNotExist:
        return render_to_response("create.html", {"page_name" : page_name})

    return render_to_response("view.html", {"page_name" : page_name,
                                            "content" : page.content})

def edit_page(request, page_name):
    c = {}
    c.update(csrf(request))
    try:
        page = Page.objects.get(pk=page_name)
        contents = page.content
    except Page.DoesNotExist:
        content = ""
    return render_to_response("edit.html", {"page_name" : page_name,
                                            "content" : content}, c)


def save_page(request, page_name):
    content = request.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 + "/")

TypeError at /wikicamp/start/edit/
pop expected at least 1 arguments, got 0


推荐答案

你还不太了解步骤3 这里:您需要使用RequestContext来确保上下文处理器运行。

You haven't quite understood step 3 here: you need to use RequestContext to ensure that the context processor is run.

return render_to_response("edit.html", {"page_name" : page_name,
                                        "content" : content},
                           context_instance=RequestContext(request))

因此,您不需要 c.update(CSRF(请求)) 位。

这篇关于csrf_token的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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