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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:05:19+00:00 2026-05-22T18:05:19+00:00

I have a simple requirement for creating a dynamic form in Django – I’ve

  • 0

I have a simple requirement for creating a dynamic form in Django – I’ve seen many examples but they seem to be incomplete, or require more extensive knowledge of Python and Django than I have! None show how the dynamic portion of the example should be called:

This is the form class with Q1 and Q2 – I place a button on the form to add another
field called Q3 – and then Q4 if pressed again: I think I got the init function semi correct:

class testform(forms.Form):
    Q1 = forms.CharField()
    Q2 = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(testform,self).__init__(*args,**kwargs)
        self.fields['Q%s'%i] = forms.CharField(label='Q%i' % i)

I want to place a button on the form to add another
field called Q3 and then Q4 if pressed again.

  • How do I call the _init_function from the view to add these fields?
  • If I use Javascript to add the field dynamically, how do I call init so that I can correctly validate with Django when I POST the form?
  • 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-22T18:05:19+00:00Added an answer on May 22, 2026 at 6:05 pm

    To answer your question: Whenever you initialize your form, it calls init(). Ex: form = testform(). That initializes your form. What I would do here is add some javascript to create these new fields. I’m not sure how familiar you are with javascript, but I’m going to assume you can figure this one out on your own.

    As for the validation, this can be done in the form. First though, you’ll want to tell it that you’ve added more fields using init().

    class TestCharField(forms.CharField):
        def __init__(self, *args, **kwargs):
            super(TestCharField, self).__init__(*args, **kwargs) # Needs * and **
        def clean(self, value):
            super(TestCharField, self).clean(value)
            # Do custom validation in here
            return value
    
    class testform(forms.Form):
        def __init__(self, *args, **kwargs):
            super(testform, self).__init__(args, kwargs)
    
            # args should be a list with the single QueryDict (GET or POST dict)
            # that you passed it
            for k,v in args[0].items():
                if k.startswith('Q') and k not in self.fields.keys():
                    self.fields[k] = TestCharField(initial=v, required=True)
    

    You can name your field whatever you want, it doesn’t have to be TestCharField. Just make sure to rename it in both places. This will allow you to make as many fields as you like. Let me know if this doesn’t quite work for you.

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

Sidebar

Related Questions

My SQL is rusty -- I have a simple requirement to calculate the sum
I have a requirement to create a simple database in Access to collect some
I develop tools in Autodesk Maya. Many of the tools I build have simple
I have a simple requirement, i need a map of type . however i
We have a customer who needs some simple forms (probably we would be creating
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,

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.