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

  • SEARCH
  • Home
  • 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 6169381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:50:54+00:00 2026-05-23T22:50:54+00:00

When the clean() method of my Django form detects an error, I’d like the

  • 0

When the clean() method of my Django form detects an error, I’d like the form fields to redisplay as empty instead of filling in whatever input the user provided.

How do I do this? Is it a property of the form field itself, or is it done in the clean() method? I tried manipulating self.cleaned_data[‘fieldname’], but that did not work.

Thanks!

  • 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-05-23T22:50:55+00:00Added an answer on May 23, 2026 at 10:50 pm

    You need to set the data attribute of the field to ” in your clean method:

    class MyForm(forms.Form):
        name = forms.CharField(max_length=50)
    
        def clean_name(self):
            name = self.cleaned_data['name']
            if name == 'Bob':
                raise forms.ValidationError(u'Name cannot be "Bob"')
                self.data['name'] = ''
            return name
    

    Hope that helps you out.

    [Edit]

    Here’s an expanded example that is working for me in Django 1.3.

    #models.py
    from django.db import models
    
    class ContactRequest(models.Model):
        name = models.CharField(max_length=100)
        email = models.EmailField()
        subject = models.CharField(max_length=255)
        message = models.TextField()
        response_returned = models.BooleanField(default=False)
    
        def __unicode__(self):
            return self.name
    
    
    #forms.py
    class ContactRequestForm(forms.ModelForm):
        class Meta:
            model = ContactRequest
            exclude = ('response_returned',)
    
        def clean_email(self):
            email = self.cleaned_data['email']
            if email != 'test@test.com':
                self.data['email'] = ''
                raise forms.ValidationError(u'Email is not test@test.com')
            return email
    

    [Edit]
    Here’s an additional example that clears all fields if a condition isn’t met in a clean method. This works for me in Django 1.1.4.

    #forms.py
    from django import forms
    
    class ContactForm(forms.Form):
        name = forms.CharField(max_length=50)
        email = forms.EmailField()
    
        def clean(self):
            cleaned_data = self.cleaned_data
            name = cleaned_data.get('name')
            email = cleaned_data.get('email')
    
            if email == 'test@test.com' and name == 'test':
                for k, _ in self.fields.iteritems():
                    self.data[k] = ''
                raise forms.ValidationError(u'Email cannot be "test@test.com" and name cannot be "test"')
            return cleaned_data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am overriding the clean() method on a Django form. I want to have
I'm using the standard authentication form. It's clean method looks like this : def
I am trying to request.user for a form's clean method, but how can I
I would like to be able to access request object in django admins clean()
Is there a clean method of mocking a class with generic parameters? Say I
Is there a C/C++/STL/Boost clean method to convert a date time string to epoch
I want to redefine the required attribute for a field in a clean method
Is there a clean and simple method of formatting an integer which is a
Is there a clean, preferably standard method of trimming leading and trailing whitespace from
I was wondering if it was possible to do form validation in django views.

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.