Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:35:51+00:00 2026-06-14T17:35:51+00:00

I’ve got a relatively standard RegistrationForm that looks like this: class RegisterForm(forms.Form): username =

  • 0

I’ve got a relatively standard RegistrationForm that looks like this:

class RegisterForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'username'}), initial='')
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'email'}), initial='')
    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'password'}), initial='')
    password_repeat = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'retype password'}), initial='')

How could I create a clean method that returns an error when a user forgets to fill in one or more fields? (ie. “You forgot to fill in the email field”)

I’ve tried the following two options in my clean() method (I’ll use the password and password_repeat fields as examples):

password = self.cleaned_data['password']
password_repeat = self.cleaned_data['password_repeat']
# initial values are set to '' for all fields, see above.
if password == '':
    raise forms.ValidationError("You forgot to type in a password.")
elif password_repeat == '':
        raise forms.ValidationError("You forgot to retype your password.")

The first option returns:

KeyError at /homepage/

‘password’


try:
    password = self.cleaned_data['password']
    password_repeat = self.cleaned_data['password_repeat']
except KeyError(password):
    raise forms.ValidationError("You forgot to fill in the password field.")

The second option returns:

UnboundLocalError at /homepage/

local variable ‘password’ referenced before assignment


Bonus points if you can provide a solution that allows for the remaining fields to be checked as well (so that I can return an form bound to the data the user successfully submitted).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:35:52+00:00Added an answer on June 14, 2026 at 5:35 pm

    You can use the required property available for all Field types, which automatically does this type of validation. So your code would look like:

    class RegisterForm(forms.Form):
        username = forms.CharField(
            widget = forms.TextInput(attrs = {'placeholder': 'username'}),
            required = True)
        email = forms.EmailField(
            widget = forms.TextInput(attrs = {'placeholder': 'email'}),
            required = True)
        password = forms.CharField(
            widget = forms.PasswordInput(attrs = {'placeholder': 'password'}),
            required = True)
        password_repeat = forms.CharField(
            widget = forms.PasswordInput(attrs = {'placeholder': 'retype password'}),
            required = True)
    

    Note: I think you can leave out those initial = '' parameters as well, as shown above.

    I’m actually not sure why you’re getting the errors you mentioned in your question, perhaps you could post the relevant code from your views.py? It could be because you need to return cleaned_data at the end of any clean method you implement.

    I would also just say that your use of the clean method is not quite right. If you refer to this page of the documentation on form and field validation you see that to validate a single field you use the specific clean_<fieldname> method e.g. clean_password_repeat. Using the clean method is appropriate when validation involves multiple field simultaneously, one example of this you may like to use is checking the inputs for the two password fields match.

    class RegisterForm(forms.Form):
        # field definitions (above)
    
        def clean(self):
            password = self.cleaned_data['password']
            password_repeat = self.cleaned_data['password_repeat']
            if password != password_repeat:
                raise forms.ValidationError(u"Passwords do not match.")
            return cleaned_data
    

    Note: Code is not tested.

    I hope this was helpful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.