问题描述
我正在构建这个维基帖子,当我尝试保存数据时遇到错误。
我目前使用的是django 1.4.3,我使用的教程很旧。所以我不认为CSRF被包含在旧版本中。
I'm building this wiki post and I encountered an error when ever I try to save data.I'm using django 1.4.3 at the moment and the tutorial i'm using is pretty old. So I don't think CSRF was included in the older version.
Forbidden (403)
CSRF verification failed. Request aborted.
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:
Your browser is accepting cookies.
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.
我认为问题出现在我的模板中,但我会列出我的views.py
I think the problem is in my templates but I'll list my views.py anyway
我的观点是:
from wiki.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
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})
content = page.content
return render_to_response("view.html",{"page_name":page_name , "content":content})
def edit_page(request,page_name):
try:
page = Page.objects.get(pk=page_name)
content = page.content
except Page.DoesNotExist:
content = ""
return render_to_response("edit.html",{"page_name":page_name, "content":content})
def save_page(request , page_name):
content = request.POST.get('content', 'this is the default')
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 + "/")
我的create.html
My create.html
<html>
<head>
<title>{{page.name}} - Create </title>
</head>
<body>
<h1>{{page_name}} </h1>
This page does not exist. <a href="/wikicamp/{{page_name}}/edit/">Create? </a>
</body>
</html>
我的edit.html,我在里面添加了{%csrf_token%},但似乎失败了。 p>
My edit.html , I added the {% csrf_token %} inside but seem to fail.
<html>
<head>
<title>{{page_name - Editing</title>
</head>
<body>
<h1>Editing {{page_name}} </h1>
<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>
</body>
</html>
我的views.py模板
My views.py template
<html>
<head>
<title>{{page_name}}</title>
</head>
<body>
<h1>{{page_name}} </h1>
{{content}}
<hr/>
<a href="/wikicamp/{{page_name}}/edit/">Edit this page ?</a>
</body>
</html>
我的URLconf:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^wikicamp/(?P<page_name>[^/]+)/edit/$','wiki.views.edit_page'),
url(r'^wikicamp/(?P<page_name>[^/]+)/save/$','wiki.views.save_page'),
url(r'^wikicamp/(?P<page_name>[^/]+)/$','wiki.views.view_page'),
)
如何解决这个问题?
推荐答案
将context_instance = RequestContext(请求)添加到您将使用其中的表单的每个视图:
看起来你没有传递上下文处理器
Add context_instance=RequestContext(request) to every view that you will use a form inside it:It seems you are not passing the context processor
from wiki.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
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})
content = page.content
return render_to_response("view.html",{"page_name":page_name , "content":content}, context_instance=RequestContext(request))
def edit_page(request,page_name):
try:
page = Page.objects.get(pk=page_name)
content = page.content
except Page.DoesNotExist:
content = ""
return render_to_response("edit.html",{"page_name":page_name, "content":content}, context_instance=RequestContext(request))
def save_page(request , page_name):
content = request.POST.get('content', 'this is the default')
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 + "/")
尝试这个。
仍然你得到的问题,请张贴urls .py还
Still you are getting the problem please post the urls .py also
这篇关于Django CSRF验证失败。请求中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!