Hi I have a very bizarre problem. I have a very simple registration form, which doesn't react at all when filling out. I can't see any errors logged. And because the submit button doesn't react at all I assume that the
if request.method == 'POST':
fails silently.
Do you see any errors? What do I miss?
forms.py
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))
def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_("The username already exists. Please try another one."))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields did not match."))
return self.cleaned_data
views.py:
from django.shortcuts import render
from accounts.forms import RegistrationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.models import User
@csrf_protect
def ClientRegistration(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']
)
user.save()
return HttpResponseRedirect('/accounts/RegSuccess/')
else:
form = RegistrationForm()
#context = {'form': form}
#return render(request, 'registration.html', context)
variables = RequestContext(request, {
'form': form
})
return render_to_response(
'registration.html',
variables,
)
registration.html
<section id="about">
<div class="container-fluid">
<div class="row text-center">
<div class="col-lg-12">
<hr class="colored">
</div>
</div>
<div class="container-fluid">
<div class="row text-center content-row">
<form action="/accounts/ClientRegistration/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</section>