I am trying out a simple user registration form in Django. CSRF token is not getting generated when I use render_to_response
return render_to_response('registration/register.html', RequestContext(request, {'form': RegistrationForm()}))
where as, CSRF token is getting generated when I use render
return render(request, 'registration/register.html', {'form':RegistrationForm()})
Am I doing anything wrong with render_to_response
Following is the relevant code block
views.py
@csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(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('/register_success/')
else:
return render_to_response('registration/register.html', RequestContext(request, {'form': RegistrationForm()}))
register.html
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
<table border="0">
{{ form.as_table }}
</table>
<input type="submit" value="Register" />
</form>
{% endblock %}