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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:14:50+00:00 2026-05-30T04:14:50+00:00

Although this is django related, it’s really just a general, programming efficiency question. I

  • 0

Although this is django related, it’s really just a general, programming efficiency question.

I have a template that, depending on the scenario, will get and return either one or two forms. If it returns one form, the variable that holds the second form will be None.

I need to check if the forms are valid. Similar to an example I read online, I’m checking .is_valid() on both forms in the same if statement. But this unsurprisingly throws an error (NoneType object has no attribute is_valid) if the second form is None.

What is the best way to check that both forms are valid without incurring an error if the second form is None?

if request.method == 'POST':

    form = form_dict[modelname][1](request.POST) 
    try:
        form_two = form_dict[modelname][2](request.POST)
    except:
        form_two = None

    if form.is_valid() and form_two.is_valid():
        # Do some stuff and save the data from the form(s)      
    else:
        try:
            form = form_dict[modelname][1]()
            form_two = form_dict[modelname][2]()
        except:
            form_two = None
  • 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-30T04:14:51+00:00Added an answer on May 30, 2026 at 4:14 am

    In the general case of checking if something is None.. just check it?

    if x is not None and x.is_valid():
       ...
    

    In Python, or and and both short circuit, meaning that in the expression x and y, y is not evaluated if x is false (since the expression is false no matter what y is).

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

Sidebar

Related Questions

I have a HashMap (although I guess this question applies to other collections) of
Alright, this is probably a really silly question but I am new to Python/Django
I have a Django app that stores client data. Currently, there are just over
Although this question and this question are close to what I'm asking, I believe
This is a follow on from my previous question although this is about something
I have the SQL2008 version of SMO and although it uses this class internally
I'm working with ExpertPDF's Html-to-PDF conversion utility for this question (although I'm open to
I'm not sure if this is a bug or I'm just missing something (although
In a Django template, I have a function function show_graph(i){ $(#+i+seegraph).click(function(){ $(#+i+graph).show(); var qaa
Django documents give this example of associating extra data with a M2M relationship. Although

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.