Chidananda Nayak
Chidananda Nayak

Reputation: 265

How to render two different Forms in one Template?

I'm a newbie in Django. Please be helping out.I have written two forms in forms.py

forms.py

from django import forms
class MyForm(forms.Form):
    PRODUCT_SIZE = (
        ('1', 'US letter 8.5x11 in'),
        ('2', 'US Trade 6x9 in'),
        ('3', 'commic book 6.63x10.25 in'),
        ('4', 'pocket book 4.25x6.88 in')
    )
    my_choice_field = 
forms.ChoiceField(choices=PRODUCT_SIZE,widget=forms.RadioSelect())


class BindingForm(forms.Form):
    BINDING = (
        ('1', 'Coil Bound Paperback'),
        ('2', 'Perfect Bound Paperback'),
        ('3', 'Saddle Stitch Paperback'),
    )
    my_binding_choice = forms.ChoiceField(choices=BINDING, 
widget=forms.RadioSelect())

views.py

from django.shortcuts import render,render_to_response
from .forms import MyForm,BindingForm

def my_view(request):
    form = MyForm()
    return render(request,'base.html',{'form': form})


def my_binding_view(request):
    form = BindingForm()
    return render(request,'base.html',{'binding_form':form})

templates/base.html

<h2>Paper Size</h2>
{% for ele in form %}
    <div class="myradiolist">
        {{ ele }}
    </div>
{% endfor %}

<h2>Binding</h2>
{% for ra in Binding_form %}
    <div class="my2ndradiolist">
        {{ ra }}
    </div>
{% endfor %}

But its only rendering First part of template class called "my radio list " to the front end while only showing the part of the second class and nothing else after that. where is the problem? or im doing whole thing wrong? how can i show 2nd part of the template in same template as when i show 2nd part in different template,it renders...

thanks in advance

Upvotes: 0

Views: 250

Answers (1)

Alasdair
Alasdair

Reputation: 309089

Each url is handled by only one view, so you need to write a single view and include both forms in the template context.

def my_view(request):
    form = MyForm()
    binding_form = BindingForm()
    return render(request,'base.html',{'form': form, 'binding_form': binding_form})

Upvotes: 2

Related Questions