python - Forbidden (403) CSRF verification failed. Request aborted -
i created custom template form signup, , whenever try signup in django application. error message csrf verification failed. request aborted.
i created custom template form signup, , whenever try signup in django application. error message csrf verification failed. request aborted.
csrf token missing or incorrect. don't again. have not being able pass error.
views.py
from django.shortcuts import render_to_response django.shortcuts import render, get_object_or_404 django.http import httpresponse, httpresponseredirect django.core.urlresolvers import reverse django.template import requestcontext django.contrib.sites.shortcuts import get_current_site django.utils.encoding import force_bytes, force_text django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode django.template.loader import render_to_string .tokens import account_activation_token django.core.mail import emailmessage .forms import signupform def index(request): return render_to_response('accounts/index.html') def register(request): if request.method == "post": form = signupform(request.post) if form.is_valid(): username = request.post.get('uname') first_name = request.post.get("fname") last_name = request.post.get("lname") email = request.post.get("email") password = request.post.get("password") dob = request.post.get("dob") gender = request.post.get("optradio") new_user = signup('username', 'first_name', 'last_name', 'email', 'password', 'dob', 'gender') new_user.is_active = false new_user.save() current_site = get_current_site(request) message = render_to_string('acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) mail_subject = 'activate linkzone account.' to_email = form.cleaned_data.get('email') email = emailmessage(subject, message, to=[to_email]) email.send() return httpresponse('please confirm email address complete registration') def activate(request, uidb64, token): try: uid = force_text(urlsafe_base64_decode(uidb64)) user = user.objects.get(pk=uid) except(tryerror, valueerror, overflowerror, user.doesnotexist): user = none if user not none , account_activation_token.check_token(user, token): user.is_active = true user.save() login(request, user) #return redirect('home') return httpresponse('thank email confirmation. can login in account.') else: return httpresponse('activation link invalid')
models.py
from __future__ import unicode_literals django.contrib.auth.models import user import uuid django.db import models class signup(user): gender = ( ('m', 'male'), ('f', 'female') ) gender = models.charfield(max_length = 50, choices = gender, null = true) slug = models.slugfield('slug', max_length = 100, unique=true) def __unicode__(self): return self.firstname def save(self, **kwargs): slug_str = "%s, %s" % (self.user, self.uuid.uuid4()) unique_slugify(self, slug_str) super(signup, self).save(**kwargs)
forms.py
from django.forms import modelform .models import signup django.contrib.auth.forms import usercreationform django import forms class signupform(usercreationform): email = forms.emailfield(max_length = 200, help_text = 'required') def __init__(self, *args, **kwargs): super(signupform, self).__init__(*args, **kwargs) class meta: model = signup fields = ("username", "email", "password1", "password2")
base.html
<form method = 'post' action = "{% url 'user-register' %}"> {% csrf_token %} <input type="text" name = "uname" class = "form-control" placeholder="user name" required> <input type="text" name = "fname" class = "form-control" placeholder="first name" required> <input type="text" name = "lname" class = "form-control" placeholder="last name" required> <input type="email" name = "email" class = "form-control" placeholder="email" required> <input type="password" name = "password1" class = "form-control" placeholder="password" required> <input type="password" name = "password2" class = "form-control" placeholder="confirm password" required> <input type="date" name = "dob" class="form-control" required> <div class="radio" required> <label><input type="radio" name="optradio" value="m">male</label> <label><input type="radio" name="optradio" value="f">female</label> </div> <button type="submit" name="register" id="btn-bevel" class="center-block">sign up</button> </form>
your index view, presumably 1 rendering template, using render_to_response
. should not use that. use render
, pass request:
return render(request, 'accounts/index.html', {})
Comments
Post a Comment