working with multiple html forms django

# views.py
def _get_form(request, formcls, prefix):
    data = request.POST if prefix in request.POST else None
    return formcls(data, prefix=prefix)

class MyView(TemplateView):
    template_name = 'mytemplate.html'

    def get(self, request, *args, **kwargs):
        return self.render_to_response({'aform': AForm(prefix='aform_pre'), 'bform': BForm(prefix='bform_pre')})

    def post(self, request, *args, **kwargs):
        aform = _get_form(request, AForm, 'aform_pre')
        bform = _get_form(request, BForm, 'bform_pre')
        if aform.is_bound and aform.is_valid():
            # Process aform and render response
        elif bform.is_bound and bform.is_valid():
            # Process bform and render response
        return self.render_to_response({'aform': aform, 'bform': bform})

# mytemplate.html
<form action="" method="post">
    {% csrf_token %}
    {{ aform.as_p }}
    <input type="submit" name="{{aform.prefix}}" value="Submit" />
    {{ bform.as_p }}
    <input type="submit" name="{{bform.prefix}}" value="Submit" />
</form>

0
0
Phoenix Logan 186120 points

                                    if request.method == 'POST':
    form1 = Form1( request.POST,prefix=&quot;form1&quot;)
    form2 = Form2( request.POST,prefix=&quot;form2&quot;)
    
    if form1.is_valid():
       # save them    
       
       # context['form1_message'] = 'Form1 saved'
    else: 
       #save them into context
       context['form1']= form1
    
    if form2.is_valid():
       # save them    
       # context['form2_message'] = 'Form2 saved'
    else: 
       #save them into context
       context['form2']= form2

    if form1.is_valid() and  form2.is_valid(): 
       #that's mean both form is valid and saved successfully 
       return redirect('page')
    else:
        return render('/page', context)


else:
    form1 = Form1(prefix=&quot;form1&quot;)
    form2 = Form2(prefix=&quot;form2&quot;)

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
multiple form handeling django working with multiple html forms django one submit button for multiple forms django how to render two different forms in the same view in django but submit differently how to render two different forms in the same iew in django multiple form in django django multiple forms Django multiple forms in one class based view how to add multiple forms in django Multiple Forms on one forms in Django multiple form in one form django create two forms in one django view having multiple form in one django template form multichoicefied in django forms two forms in POST django how to get two form value in one submit in django django form helper add submit to specific form when multiple forms on same page django multiple forms one page django multiple forms on a page handling multiple forms in django multiple forms in single view django how to create single form for multiple pages in django multistep forms django how to identify forms in case of multiple forms in django django formset add multiple of the same form Django multiple forms for one model multi page forms django process multiple forms in the samge page django submit multiple forms at once in django several submit buttons in form django template use two django form on the same page django with 2 submit on form template How to use two different Django Form at the same template multiple forms in one page django how to use two submit buttons in one form in django forms with multitple model djang django form two submit buttons django forms field field multiple django multiple formset multiple form data django multiple forms in django 2 forms in one html page django django multi form example multiple forms on one page django django multiple forms on one page django multiple form in one view submit two form with same button django django form two buttons in one form only one should work how to understand which form is submitted if i have multiple forms in a page django multiple form django Django two forms one submit django two forms on one page use one submit button for 2 forms django django forms multiwidget submit multiple forms django how to get 2 forms in django in one html page Proper way to handle multiple forms on one page in django django form with multiple models django form multiple fields two forms in one page django multiplepage forms django how to have two forms in one view in django how to show multiple forms for multiple models django django forms can i include two forms in one view use multiple form django CBV use multiple form django how to handle two forms in one page django how can i add multiple forms in one page django django two form in one template django two form in one html how to add multiple in django form multiple forms django multiple forms view.py django django view multiple forms how to use multiple forms in django single views use 2 forms in django django use multiple forms in view two forms in one template django
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source