Python Answers

How to handle multiple forms on one page in Python Django?

Spread the love

To handle multiple forms on one page in Python Django, we can set the forms to use in our views.

For instance, we write

if request.method == 'POST':
    bannedphraseform = BannedPhraseForm(request.POST, prefix='banned')
    if bannedphraseform.is_valid():
    bannedphraseform = BannedPhraseForm(prefix='banned')

if request.method == 'POST' and not bannedphraseform.is_valid():
    expectedphraseform = ExpectedPhraseForm(request.POST, prefix='expected')
    bannedphraseform = BannedPhraseForm(prefix='banned')
    if expectedphraseform.is_valid():

    expectedphraseform = ExpectedPhraseForm(prefix='expected')

in our view to set different forms to use according to different conditions.

We use if statements to check request methods and form validation to assign which form to render in the page.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *