0

我想{% 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 %}。谢谢你的想法!

4

1 回答 1

0

发生这种情况是因为您正在渲染browse-year.html

return render(request, 'browse-year.html', {'form':form, 'thing_list':thing_list})

您应该渲染另一个 html,即您没有给出名称但以

{% extends 'base.html' %}

只有通过渲染您扩展基础的内容,其他内容才会出现。

于 2013-07-08T18:21:51.860 回答