Search code examples
djangopython-3.xdjango-authentication

Django , password dont match error message not displaying. Customised UserAuth app


I am having issues with an error being "not raised" within a register form page. It seems to pulling the form again but without "passwords do not matched" message.

my form handler code

from django import forms 

from accounts.models import User

class RegistrationForm(forms.ModelForm):
    """
    Form for registering a new account.
"""
email = forms.EmailField(widget=forms.TextInput,label="Email")
password1 = forms.CharField(widget=forms.PasswordInput,
                            label="Password")
password2 = forms.CharField(widget=forms.PasswordInput,
                            label="Password (again)")

class Meta:
    model = User
    fields = ['email', 'password1', 'password2']

def clean(self):
    """
    Verifies that the values entered into the password fields match

    NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field.
    """
    cleaned_data = super(RegistrationForm, self).clean()
    if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
        if self.cleaned_data['password1'] != self.cleaned_data['password2']:
            raise forms.ValidationError("Passwords don't match. Please enter both fields again.")
    return self.cleaned_data

def save(self, commit=True):
    user = super(RegistrationForm, self).save(commit=False)
    user.set_password(self.cleaned_data['password1'])
    if commit:
        user.save()
    return user

my views code:

def register(request):
"""
User registration view.
"""
if request.method == 'POST':
    form = RegistrationForm(data=request.POST)
    if form.is_valid():
        user = form.save()
        return redirect('/')
else:
    form = RegistrationForm()
return render_to_response('accounts/register.html', {
    'form': form,
}, context_instance=RequestContext(request))

I setup the register to be in the root of the app '/'

patterns in account app

from django.conf.urls import url from accounts import views

 urlpatterns = [
  url(r'^$', views.register, name='register'),
  #url(r'^register$', views.register, name='register'),
  url(r'^login$', views.login, name='login'),
  url(r'^logout$', views.logout, name='logout'),

]

in main:

urlpatterns = [
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),

    # sets login/register to root url
    url(r'^', include('accounts.urls', namespace ='accounts')),
    url(r'^admin/', include(admin.site.urls))

]

The accounts app sets the username to be an email for djangos built in userAuth. All other fields fill in on the html page such as "invalid email address"

I have tried multiple things but have failed so far, any help would be appreciated at this stage.

Thank you.

I am using ubuntu 14, python 3.4 django 1.8, virtualenv


Solution

  • You are using:

    raise forms.ValidationError("Passwords don't match...')
    

    so this is a global error; it won't show next to a specific field. Make sure that your template accounts/register.html show these errors.


    Also just to make sure; you can print something just before that raise (just for debugging) so you can confirm that the validation occurred in the first place.