我是Django的新手。请帮忙。我在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})
模板/ 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 %}
但它唯一渲染模板类的第一部分称为“我的收音机列表”到前端,而只显示第二类的一部分,之后没有别的。问题出在哪儿?或者我做错了什么?如何在同一个模板中显示模板的第二部分,就像我在不同的模板中显示第二部分一样,它呈现...
提前致谢
答案 0 :(得分:2)
每个网址只由一个视图处理,因此您需要编写单个视图并在模板上下文中包含这两种形式。
def my_view(request):
form = MyForm()
binding_form = BindingForm()
return render(request,'base.html',{'form': form, 'binding_form': binding_form})