On Sat, Jul 6, 2013 at 9:38 AM, Kakar Arunachal Service <
[email protected]> wrote:
> Hello!
> I am getting CSRF verification failed error, even after i added {%
> csrf_token %} tag in the html file.
>
> Here's my html file:
>
> {% extends "base.html" %}
> {% block title %} User Registration {% endblock %}
> {% block head %} User Registration {% endblock %}
> {% block content %}
> <form method = "post" action = ".">
> {% csrf_token %}
> {{ form.as_p }}
> <input type ="submit" value="register" />
> </form>
> {% endblock %}
>
> and my views.py:
>
> def register_page(request):
> if request.method == 'POST':
> form = RegisrationForm(request.POST)
> if form.is_valid():
> user = User.objects.create_user(
> username = form.cleaned_data['username'],
> password = form.cleaned_data['password1'],
> email = form.cleaned_data['email']
> )
> return HttpResponseRedirect('/')
> else:
> form = RegisrationForm()
> variables = RequestContext(request, {
> 'form': form
> })
> return render_to_response('registration/register.html',
> variables)
> else:
> return render_to_response('registration/register.html')
>
> else:
return render_to_response("registration/register.html",
RequestContext(request, {})
> Where am i doing wrong???
>
In your outer else statement, you need to return a RequestContext instance
..
You could also use the 'render' shortcut:
from django.shortcuts import render
.....
return render(request, "registration/register.html")
Basically, you need to make sure the template has the context created by
RequestContext, otherwise csrf_token will not be available as a variable on
the template.
Hope that helps,
Sanjay
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.