简体   繁体   中英

Django UserCreationForm not rendering errors in template

I am using django's UserCreationForm to sign up users. It works perfectly except for the errors. I cannot get them to render. I don't think there is anything wrong with the template as I have tried this with the most basic of templates and using form.as_p and form.as_table and still the same the registration works but if you put 2 different passwords in it just refreshes the screen with an empty form and no errors. Also I have tried sending the form.errors through the django messages and it passes the correct error when there is one but this solution is not practical for me.

It wont let me post the template because of indenting. I am using {{form.non_field_errors}} at the top of the form and then {{ form.email.error }} etc.

Please help if you can:)

Form class..

class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)

class Meta:
    model = User
    fields=('username', 'email', 'password1', 'password2')

def save(self, commit=True):
    User= super(MyRegistrationForm, self).save(commit=False)
    User.email = self.cleaned_data["email"]
    if commit:
        User.save()

    return User

View method...

def home(request):
args = {}
args.update(csrf(request))
if request.method =='POST':
    form = MyRegistrationForm(request.POST)
    if form.is_valid():
        ##save_it = form.save(commit=False)
        form.save()
        messages.success(request, 'Thank you for joining!')
        #return HttpResponseRedirect('thank-you')

        return render_to_response('thankyou.html', locals(), context_instance=RequestContext(request))
    else:
        args['form'] = MyRegistrationForm()  
        return render_to_response('signup.html', args, context_instance=RequestContext(request))
args={}
args.update(csrf(request))
args['form'] = MyRegistrationForm()

context = RequestContext(request,
                       {'user': request.user})
return render_to_response('signup.html', args,
                         context_instance=context)

You are explicitly recreating the form if it has errors, replacing it in the context with one that is unbound and therefore doesn't have errors. Don't do that. Your view should be simply:

def home(request):
    if request.method =='POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Thank you for joining!')
            return HttpResponseRedirect('thank-you')

    else:
        form = MyRegistrationForm()

    return render(request, 'signup.html', {'form': form})

Note the other changes: always redirect after a successful post; render rather than render_to_response, since it creates a RequestContext for you; and no need to add the user or csrf values yourself, since they are added by the context processors as long as you do use a RequestContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM