Django - {%csrf_token%}在模板中使用,但上下文没有提供该值 [英] Django - {% csrf_token %} was used in a template, but the context did not provide the value

查看:5625
本文介绍了Django - {%csrf_token%}在模板中使用,但上下文没有提供该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚到Django,我仍然试图掌握其功能。我已经使用 Django 1.4.2 创建了非常简单的项目,它具有简单的索引页面,您可以在输入的内容和结果页面中输入您的输入(代码如下)。



提交后,我收到错误403和以下消息:


A {%csrf_token %}在模板中使用,但上下文没有
提供该值。这通常是由于不使用
RequestContext引起的。 warnings.warn(A {%csrf_token%}在
模板中使用,但上下文没有提供该值,这通常是
,而不是使用RequestContext。)


index.html

 <!DOCTYPE html> 
< head>
< title>索引页< / title>
< / head>
< body>
< div id =header>欢迎使用索引页面< / div>
< div id =content>
< p>输入您的名称< / p>
< form action =/ result /method =postaccept-charset =utf-8> {%csrf_token%}
< input type =textname =回答>
< input type =submitvalue =Send!>
< / form>
< / div>
< / body>

result.html

 <!DOCTYPE html> 
< head>
< title>结果页< / title>
< / head>
< body>
< div id =header>这是结果< / div>
< div id =content>
< p>您的姓名是:{{answer}}< / p>
< / div>
< / body>

views.py


$ b $从django.http中导入HttpResponse
从django.shortcuts导入render_to_response
从django.template导入RequestContext

def index(请求):
返回render_to_response('index.html')

def result(request):
p = request.POST ['answer']
return render_to_response 'result.html',{'answer':p},context_instance = RequestContext(request))

我已经查看了互联网上的文档和各种示例,但是我不明白我在做错什么。如果我在 settings.py 中禁用了 django.middleware.csrf.CsrfViewMiddleware ,那么我完全可以得到我想要的东西,但这不是我要找的答案。 >

我喜欢更有经验的Django ninjas的帮助: - )

解决方案

code> index.html 在 RequestContext 之前呈现。尝试这样:

  def index(request):
return render_to_response('index.html',context_instance = RequestContext请求))

我还建议您使用更方便的快捷方式 render

从django.shortcuts import render 

def index(request):
return render('index.html ')

从文档:


render()与使用
context_instance参数的render_to_response()调用相同,强制使用RequestContext。


编辑



感谢 @nerdwaller 提到,现在需要更新的版本:

  render(request,'index.html' {PARAMS}); 


I'm new to Django and I'm still trying to get to grips with its features. I've created very simple project with Django 1.4.2 which has index page with simple form where you enter something and results page where your input is displayed after submission (the code is below).

After submission, I get error 403 and the following message:

A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.")

index.html

<!DOCTYPE html>
<head>
    <title>Index page</title>
</head>
<body>
    <div id="header">Welcome to index page</div>
    <div id="content">
        <p>Enter your name</p>
        <form action="/result/" method="post" accept-charset="utf-8">{% csrf_token %}
            <input type="text" name="answer">
            <input type="submit" value="Send!">
        </form>
    </div>
</body>

result.html

<!DOCTYPE html>
<head>
    <title>Result page</title>
</head>
<body>
    <div id="header">Here is the result</div>
    <div id="content">
        <p>Your name is: {{ answer }}</p>
    </div>
</body>

views.py

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

def index(request):
    return render_to_response('index.html')

def result(request):
    p = request.POST['answer']
    return render_to_response('result.html', {'answer': p}, context_instance=RequestContext(request))

I've looked into documentation and various examples on the Internet, but I don't understand what I'm doing wrong. If I disable django.middleware.csrf.CsrfViewMiddleware in settings.py, I get exactly what I want, but that's not the answer I'm looking for.

I appreciate help from more experienced Django ninjas :-)

解决方案

Your index.html is rendered without RequestContext. Try this:

def index(request):
    return render_to_response('index.html', context_instance=RequestContext(request))

I also recomend you to use more convenient shortcut render:

from django.shortcuts import render

def index(request):
    return render('index.html')

From docs:

render() is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.

EDIT:

Thanks @nerdwaller for mentioning, newer versions now needs:

render(request, 'index.html', {params});

这篇关于Django - {%csrf_token%}在模板中使用,但上下文没有提供该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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