我想{% include %}
在页面上包含第二个表单,但表单是空的..
视图.py:
def year_form(request):
thing_list = Thing.objects.all()
if request.method == 'POST':
form = YearBrowseForm(request.POST)
if form.is_valid():
year = form.cleaned_data['year']
return HttpResponseRedirect(reverse('browse_years', kwargs={'year':year}))
else:
form = YearBrowseForm()
return render(request, 'browse-year.html', {'form':form, 'thing_list':thing_list})
表格.py:
class YearBrowseForm(forms.Form):
year = forms.ChoiceField(choices=YEARS_EMPTY, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
网址:
url(r'^browse/years/(?P<year>\d+)/$', 'my_app.views.browse.year_form', name='browse_years'),
html:
{% extends 'base.html' %}
{% block content %}
{% include 'browse-year.html' %}
{% endblock %}
浏览年.html:
<form action='' method='post' enctype='multipart/form-data'>
{{ form.as_p }}
{% csrf_token %}
</form>
这会呈现一个空表单(即源显示正在包含一个表单,但它没有内容)。我在这里想念什么?这是这个问题的延续,我试图通过使用不同的方法来解决这个问题{% include %}
。谢谢你的想法!