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 7502397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:51:20+00:00 2026-05-29T20:51:20+00:00

I have a django charField that is checked via the is_valid() method. The user

  • 0

I have a django charField that is checked via the is_valid() method. The user is supposed to enter a valid logical expression in this field, so I wrote a parsing method that raises an exception if the expression is not correct.

How can I enhance the is_valid() method to cover this exception and display an error message to the user that his query was wrong?

I read this article (https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute) but still have no idea how to do that.

        try:
        job = Job(user=request.user)  # set the current user
        form = JobForm(request.POST, instance=job)

        if form.is_valid():
            form.save()
            job.execute()
            messages.success(request, u'A new job with the query "{query}" was created.'.format(query=job.query))
            return HttpResponseRedirect(reverse('job-index'))

        return self.render_to_response({'job_form': form, 'is_new': True})
    except ParseError:
        return self.render_to_response({'job_form': form, 'is_new': True})

The try…except-Block should be done within the is_valid() method, that is my intention. Someone got any hints?

  • 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-29T20:51:22+00:00Added an answer on May 29, 2026 at 8:51 pm

    You’ve provided an answer to the question yourself – you create your own form (or model form) and perform custom validation on that form’s field using its clean_'fieldname'() method. So for example, say your model is:

    class Job(models.Model):
        expression_field = models.CharField(...)
        ...
    

    you create a forms.py:

    class JobForm(forms.ModelForm):
        pass
        class Meta:
            model = Job 
    
        def clean_expression_field(self):
             # You perform your custom validation on this field in here, 
             # raising any problems
             value = self.cleaned_data['expression_field']
             if value is 'really_bad':
                  raise forms.ValidationError("bad bad bad")
             return value
    

    then make use of it in your views.py as you already are in your example. Now if the value the user enters doesn’t meet your criteria, an exception will be automatically raised

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

Sidebar

Related Questions

I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a django model and model form that look like this: -models.py class
I want to have a django model like this: class Model(models.Model): string = models.CharField()
I have a Django app that allows the user to create variables and name
I have a simple django app that is using only the admin. This is
I have the following model in django: class Node(models.Model): name = models.CharField(max_length=255) And this
I have a model that looks like this: class Category(models.Model): name = models.CharField(max_length=60) class
I have a django form that allows a user to select multiple options: CARDS

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.