Django KeyError at /register/ -


i have registration let users register , i'm having difficulty fixing it.

the problem when user submits single field instead of whole form example email . error

keyerror @ /register/  password request method:     post request url:    http://127.0.0.1:8000/register/ file "c:\python26\lib\site-packages\django\forms\forms.py" in _get_errors   115.             self.full_clean() file "c:\python26\lib\site-packages\django\forms\forms.py" in full_clean   271.         self._clean_form() file "c:\python26\lib\site-packages\django\forms\forms.py" in _clean_form   299.             self.cleaned_data = self.clean() file "c:\o\17\mysite\pet\forms.py" in clean   31.       if self.cleaned_data['password'] != self.cleaned_data['password1']:  exception type: keyerror @ /register/ exception value: password 

i tried fix solution using if . if user has submitted username or other required field , process form otherwise redisplay original form.

but still same error.

this edited views.py (at bottom of page original registrationform)

def petregistration(request):        if request.user.is_authenticated():         return httpresponseredirect(reverse('world:happyland'))      if request.method =='post':         form = userregistration(request.post)     if form.is_valid():         username = form.cleaned_data['username']         if username:             email=form.cleaned_data['email']             if email:                 password=form.cleaned_data['password']                 if password:                     user = user.objects.create_user(             username=form.cleaned_data['username'],              email=form.cleaned_data['email'],              password=form.cleaned_data['password']             )                     user.is_active = true                     user.first_name = form.cleaned_data['name']                     user.save()                      person = authenticate(             username=form.cleaned_data['username'],              password=form.cleaned_data['password']             )                      person.objects.create(user_id=user.id,                      name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday'])                      login(request, person)                     return httpresponseredirect(reverse('world:happyland'))  return render(request, 'register.html', {'form': userregistration()}) 

how can fix error , how display error message on other fields user didn't fill out "error missing field , please fill field".

def petregistration(request):    if request.user.is_authenticated():     return httpresponseredirect(reverse('world:happyland'))  if request.method =='post':     form = userregistration(request.post)     if form.is_valid():         user = user.objects.create_user(             username=form.cleaned_data['username'],              email=form.cleaned_data['email'],              password=form.cleaned_data['password']             )         user.is_active = true         user.first_name = form.cleaned_data['name']         user.save()          person = authenticate(             username=form.cleaned_data['username'],              password=form.cleaned_data['password']             )          person.objects.create(user_id=user.id,              name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday'])          login(request, person)         return httpresponseredirect(reverse('world:happyland'))  return render(request, 'register.html', {'form': userregistration()}) 

my forms.py

class userregistration(forms.form):     username = forms.charfield()     name = forms.charfield()     email = forms.emailfield()     birthday = forms.datefield(widget=extras.selectdatewidget(years=range(1950, 2012)))      password = forms.charfield(         widget=forms.passwordinput(render_value=false)         )     password1 = forms.charfield(         label=(u'verify password'),          widget = forms.passwordinput(render_value=false)         )      def clean_username(self):         username = self.cleaned_data['username']         try:             user.objects.get(username=username)         except user.doesnotexist:             return username         raise forms.validationerror(             "that user taken , please select ")      def clean(self):         if self.cleaned_data['password'] != self.cleaned_data['password1']:             raise forms.validationerror("the password not match ")         return self.cleaned_data 

my models.py

class person(models.model):     user = models.foreignkey(user)     name = models.charfield(max_length=100, blank=true)     birthday = models.datefield(blank=true,null=true)     def __unicode__(self):         return self.name 

problem clean(). in clean(), trying access field password on form's cleaned_data. password available on cleaned_data if user has filled field. so, must check password there in cleaned_data before trying access it.

changing clean():

def clean(self):     if 'password' in self.cleaned_data , 'password1' in self.cleaned_data , self.cleaned_data['password'] != self.cleaned_data['password1']:         raise forms.validationerror("the password not match ")     return self.cleaned_data 

you can provide keyword argument error_messages on form field showing error message "error missing field , please fill field".

class someform(forms.form):     name = forms.charfield(error_messages={'required':'error missing field , please fill field'}) 

there bug in view.

is_valid() populates errors on form same form instance must sent template can access errors on form's fields.

but in view, have 1 call render() gets called in case of invalid form on post request. , in render(), creating new instance of form. so, new form sending template not have errors.

so, making slight modification view:

def petregistration(request):        if request.user.is_authenticated():     return httpresponseredirect(reverse('world:happyland'))      form = userregistration() #this used in request     if request.method =='post':         form = userregistration(request.post) #this used in post request         if form.is_valid():             user = user.objects.create_user(                 username=form.cleaned_data['username'],                  email=form.cleaned_data['email'],                  password=form.cleaned_data['password']             )             user.is_active = true             user.first_name = form.cleaned_data['name']             user.save()              person = authenticate(                 username=form.cleaned_data['username'],                  password=form.cleaned_data['password']             )              person.objects.create(user_id=user.id,                  name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday'])              login(request, person)             return httpresponseredirect(reverse('world:happyland'))      return render(request, 'register.html', {'form': form}) 

notice in view, have added form=userregistration() before checking if post request, , have added comment @ 2 places instantiating userregistration. , in render(), should send form.

then {{form.username.errors}} work.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -