dimanche 12 avril 2015

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)

Aucun commentaire:

Enregistrer un commentaire