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.
I think the problem is in my templates but I'll list my views.py anyway
My views are :
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 + "/")
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>
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>
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>
My 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'),
)
How could I fix this prooblem?
from django.template import RequestContext
return render_to_response('contact_form.html',{'errors': errors}, context_instance=RequestContext(request))
and also use the csrf_token tag inside the element if the form is for an internal URL, e.g.:
"form action="" method="post">{% csrf_token %}"
reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With