<\/span><\/h2>\nWhen trying to access your Django site within CSRF configured you receive the following via a Forbidden (403) HTTP error message:<\/p>\n
CSRF verification failed. Request aborted.
No CSRF or session cookie.<\/pre>\n<\/span>Solution<\/strong><\/span><\/h2>\nIn my scenario I found that the order of settings.MIDDLEWARE_CLASSES was incorrect. Below shows you an example<\/p>\n
<\/span>settings.py<\/strong><\/span><\/h2>\nMIDDLEWARE_CLASSES = (
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
)<\/p>\n
<\/span>views.py<\/strong><\/span><\/h2>\nfrom django.shortcuts import render_to_response
from django.template import RequestContext<\/p>\n
def input(request):
return render_to_response(‘input.html’, context_instance=RequestContext(request))<\/p>\n
def output(request):
if ‘q’ in request.POST:
message = request.POST[‘q’]
else:
message = ‘error’
return render_to_response(‘output.html’, {‘message’: message}, context_instance=RequestContext(request))<\/p>\n
<\/span>template<\/strong><\/span><\/h2>\n<html>
<body><\/p>\n
<form action=”\/output\/” method=”POST”>
{% csrf_token %}
<input type=”text” name=”q”>
<input type=”submit” value=”input”>
<\/form><\/p>\n
<\/body>
<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"
Issue When trying to access your Django site within CSRF configured you receive the following via a Forbidden (403) HTTP error message: CSRF verification failed. Request aborted.No CSRF or session cookie. Solution In my scenario I found that the order of settings.MIDDLEWARE_CLASSES was incorrect. Below shows you an example settings.py MIDDLEWARE_CLASSES = ( ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.middleware.common.CommonMiddleware’, … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"yoast_head":"\nDjango - CSRF verification failed. Request aborted. - Fir3net<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n