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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:01:56+00:00 2026-06-10T20:01:56+00:00

This is a problem that occurred to me while working on a Django project.

  • 0

This is a problem that occurred to me while working on a Django project. It’s about form validation.

In Django, when you have a submitted form, you can call is_valid() on the corresponding form object to trigger the validation and return a Boolean value. So, usually you have code like that inside your view functions:

if form.is_valid():
    # code to save the form data

is_valid() not only validates the form data but also adds error messages to the form object that can afterwards be displayed to the user.

On one page I use two forms together and also want the data to be saved only if both forms contain valid data. That means I have to call is_valid() on both forms before executing the code to save the data. The most obvious way:

if form1.is_valid() and form2.is_valid():
    # ...

won’t work because of the short circuit evaluation of logical operators. If form1 is not valid, form2 will not be evaluated and its error messages would be missing.

That’s only an example. As far as I know, there is no greedy alternative to and/or as in other languages (i.e. Smalltalk). I can imagine that problem occurring under different circumstances (and not only in Python). The solutions I could think of are all kind of clumsy (nested ifs, assigning the return values to local variables and using them in the if statement). I would like to know the pythonic way to solve this kind of problem.

  • 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-10T20:01:58+00:00Added an answer on June 10, 2026 at 8:01 pm

    How about something like:

    if all([form1.is_valid(), form2.is_valid()]):
       ...
    

    In a general case, a list-comprehension could be used so the results are calculated up front (as opposed to a generator expression which is commonly used in this context). e.g.:

    if all([ form.is_valid() for form in (form1,form2) ])  
    

    This will scale up nicely to an arbitrary number of conditions as well … The only catch is that they all need to be connected by “and” as opposed to if foo and bar or baz: ....

    (for a non-short circuiting or, you could use any instead of all).

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

Sidebar

Related Questions

I have this problem that I can ref a button for control it. I
i'm having an weird problem, my project have a login page, that's working, but
Ok I have this problem that I've never had before, it's really bugging me.
I have this problem that my sites uses alot of ajax and when a
There was this problem that has been asked about implementing a load byte into
So we have this problem that we are trying to figure out. Heres what
The background to this problem is that I'm doing a computing project which involves
I think that this problem can be sorted using reflection (a technology which I'm
While working on a question/answer program for school, it occurred to me that I
I am having this problem An error occurred while trying to make a request

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.