[Django]-Django Forms: Validation message not showing

5👍

Since, the form is not validate in the else clause your form variable is overriden with a new form where it looses all of the errors

if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            # If form has passed all validation checks then continue to save member.
            user = User.objects.create_user(
                                            username=form.cleaned_data['username'],
                                            email=form.cleaned_data['email'], 
                                            password=form.cleaned_data['password']
                                            )
            user.save()
            #member = User.get_profile()
            #member.name = form.cleaned_data['name']
            #member.save()
            member = Member(
                            user=user,
                            name=form.cleaned_data['name']
                            )
            member.save()

            # Save is done redirect member to logged in page.
            return HttpResponseRedirect('/profile')
        return render_to_response('pageRegistration.html', context,context_instance=RequestContext(request))

2👍

Updated for the CBV world.

This is what was causing the equivalent error for me:

class CreateView(generic.CreateView): # or generic.UpdateView

    def get_context_data(self, **kwargs):
        context_data = super(CreateView, self).get_context_data(**kwargs)
        # context_data['form'] = self.form_class # << this was the problematic override

0👍

Perhaps the page is returning a new form every time it reloads. Check if the
context variable in views.py contains ‘form’ : form

👤Roy

Leave a comment