1

I want to make a signup page just like:

enter image description here

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!

2
  • @rajasimon password_1 comes from forms.SignupForm(request.POST), having nothing to do what clean_password_2 return. Commented Jan 7, 2015 at 14:16
  • @rajasimon when the two password aren't same, no error message display. Commented Jan 7, 2015 at 14:25

2 Answers 2

4

You should drop the first else clause altogether. If the form is not valid, you should not redirect, but fall through to the final render call which redisplays the form along with the errors.

Sign up to request clarification or add additional context in comments.

Comments

2

Your redirect call doesn't display the errors because it has lost it's context. It should be enough to not redirect but instead print the error for logging purposes.

Instead of:

return redirect(reverse("accounts:signup"))

Try:

print "Invalid Registration Attempt: user:{0} email:{1}".format(username, email)

The errors will then be displayed when the form is present in the context upon rendering:

return render(reverse("accounts:signup"), {'form': signup_form}) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.