django - CSRF verification failed. Request aborted. CSRF cookie not set -
error:
reason given failure: csrf cookie not set.
in general, can occur when there genuine cross site request forgery, or when django's csrf mechanism has not been used correctly. post forms, need ensure: browser accepting cookies. view function uses requestcontext template, instead of context. in template, there {% csrf_token %} template tag inside each post form targets internal url. if not using csrfviewmiddleware, must use csrf_protect on views use csrf_token template tag, accept post data. you're seeing section of page because have debug = true in django settings file. change false, , initial error message displayed. can customize page using csrf_failure_view setting.
did find, nothing helped, must foolish((
my template
<form method="post">{% csrf_token %} {% field in form %} <div class="control-group"> {{ field }} </div> {% endfor %} <input type="submit" id="fsubmit" class="btn btn-primary btn-large btn-block" value='register'/> </form>
my views.py
from django.shortcuts import render_to_response django.template import requestcontext def register_user(request): context = {} if request.method == 'post': form = registerform(request.post) if form.is_valid(): pass else: form = registerform(auto_id=false) context['form'] = form return render_to_response('templates/register.htm',context, context_instance=requestcontext(request))
settings
middleware_classes = ( 'django.middleware.common.commonmiddleware', 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware',
)
use csrf_exempt avoid csrf token.
in view
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt def register_user(request): context = {} if request.method == 'post': form = registerform(request.post) if form.is_valid(): pass else: form = registerform(auto_id=false) context['form'] = form return render_to_response('templates/register.htm',context, context_instance=requestcontext(request))
Comments
Post a Comment