You should remove the inner else: clause and the redirect() line. If is_valid() fails, the last return statement will be used, which will pass along the bound form in signup_form that was snagged inside of your first if statement. Mimic the behavior here: https://docs.djangoproject.com/en/1.7/topics/forms/#the-view
BTW, django.shortcuts.redirect should handle the reverse() for you, so you can take that out to make your code a bit cleaner. Also, not sure if this is a copy/paste issue, but I think render() requires that request be sent along as the first argument, although I don't use FBV's, so don't quote me on that. -James On Wed, Jan 7, 2015 at 6:03 AM, 赵飞 <[email protected]> wrote: > I want to make a signup page just like: > > > <https://lh5.googleusercontent.com/-BYeglZkQ0cM/VK06PkzpdPI/AAAAAAAAAN0/l5xB5VdkbJk/s1600/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%2B2015-01-07%2B%E4%B8%8B%E5%8D%889.52.08.png> > When user click signup, I want to check the if the two password are the > same, when not, give a error message after "confirm password". Here is my > code: > > *forms.py* > > > > > > > > > > > > > > > > > > > > > > > > > > *class SignupForm(forms.Form): username = forms.CahrField( > label=_("username"), max_length=30, ) email = > forms.EmailField(label=_('email'),) password_1 = forms.CharField( > label=_("password"), widget=forms.PasswordInput, ) password_2 > = forms.CharField( label=_("password_confirmed"), > widget=forms.PasswordInput, ) def clean_password_2(self): > password_1 = self.cleaned_data.get("password_1") password_2 = > self.cleaned_data.get("password_2") if password_1 and password_2 and > password_1 != password_2: raise > forms.ValidationError(_('password confirm failed')) return > password_2 signup.html* > > <form method="post" action="{% url 'accounts:signup_post' %}"> > {% csrf_token %} > <table> > {% for field in form %} > <tr> > <td>{{ field.label_tag }}</td> > <td>{{ field }}</td> > <td>{{ field.errors }}</td> > </tr> > {% endfor %} > </table> > > <input type='submit' id="submit" value={% trans "signup" %}> > <a href="{% url 'accounts:login' %}">{% trans "Already have > accounts?" %}</a> > </form> > > > > *views.py* > > def signup_post(request): > if request.method == 'POST': > signup_form = forms.SignupForm(request.POST) > > if signup_form.is_valid(): > signup_info = signup_form.cleaned_data > username = signup_info['username'] > email = signup_info['email'] > password = signup_info['password_1'] > user = User.objects.create_user( > username=username, > email=email, > password=password) > user.save() > # redirect to main page(not written so far) > else: > # I guess something wrong here, but no idea how to fix it. > return redirect(reverse("accounts:signup")) > > else: > signup_form = forms.SignupForm() > > return render(reverse("accounts:signup"), {'form': signup_form}) > > Can anyone help me out? > *Thanks!* > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/d969fed6-5041-4093-aa35-35d611993cd3%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/d969fed6-5041-4093-aa35-35d611993cd3%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUcZytC8tQk8CtH_p5PSrpCCZTszhP-W1ywhdQ61aMPKw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

