我有一个用Python / Django构建并在Heroku上部署的项目。数据库存储有关当选官员和各种html模板检索数据的信息。创建可视化。
我有一个表格,用户可以搜索选定年份的所有当选官员。得到他们的信息。
我还有另一种形式,用户可以选择受众特征(种族,性别等)并获得所有匹配结果。
按年份表单/页面搜索完全符合预期。有一个下拉菜单可供选择一年,用户点击提交,并显示该年份的数据,并且所选年份在下拉菜单中保持活动状态。
人口统计表格几乎按预期运作。用户选择种族,性别和派对,点击提交,并显示结果。但是下拉菜单会重置为默认设置。我没有看到我为这两件事设置表单,视图或html的方式有什么不同,所以我问为什么会发生这种情况。
按年度搜索 - 这有效,显示结果时表单不会重置
#forms.py
YEAR_CHOICES = (('2000', '2000'), ('2001', '2001'), ('2002', '2002'), ('2003', '2003'), ('2004', '2004'), ('2005', '2005'), ('2006', '2006'), ('2007', '2007'), ('2008', '2008'), ('2009', '2009'), ('2010', '2010'), ('2011', '2011'), ('2012', '2012'), ('2013', '2013'), ('2014', '2014'))
class SearchFormByYear(forms.Form):query = forms.ChoiceField(label = "Choose a year", widget = forms.Select, choices = YEAR_CHOICES)
#views.py
def gender_by_year(request):
male, female, query = 0, 0, 0
form = SearchFormByYear()
show_results = False
if 'query' in request.GET:
show_results = True
query = request.GET['query'].strip() #query is year
if query:
form = SearchFormByYear({'query':query})
male = Term.objects.filter(start_date__lte = query).filter(end_date__gte = query).filter(councilperson__gender__exact = 'M').count()
female = Term.objects.filter(start_date__lte = query).filter(end_date__gte = query).filter(councilperson__gender__exact = 'F').count()
else:
male = 'no data'
female = 'no data'
variables = RequestContext(request, {'form':form, 'query':query, 'male':male, 'female':female, 'show_results':show_results})
return render_to_response('year_search.html', variables)
#excerpt from year_search.html
<div id = "search-form" style = 'width:200px;'>
<form id = "search-form_box" method = "get" action = ".">
{{ form.as_p }}
<input type = "submit" value = "search" />
</form>
</div> <!-- end search form div -->
<div id = "search-results" style = 'width: 500px;'>
{% if show_results %}
YEAR: {{ query }}
<ul>
<li style = 'display:inline'>Male: {{ male }}</li>
<li style = 'display:inline'>Female: {{ female }}</li>
</ul>
{% endif %}
</div> <!-- end search results div -->
按受众特征搜索 - 这不起作用,表单会在显示结果时重置
#forms.py
RACE_CHOICES = (('All', 'All'), ('Asian', 'Asian'), ('Black', 'Black'), ('Hispanic', 'Hispanic'), ('White', 'White'), ('unknown', 'Unknown'))
GENDER_CHOICES = (('All', 'All'), ('F', 'Female'), ('M', "Male"))
PARTY_CHOICES = (('All', 'All'), ('Democrat', 'Democrat'), ('Republican', 'Republican'), ('unknown', 'Unknown'))
class FindCombo(forms.Form):
choose_gender = forms.ChoiceField(label = 'Select gender', widget = forms.Select, choices = GENDER_CHOICES)
choose_race = forms.ChoiceField(label = 'Select race', widget = forms.Select, choices = RACE_CHOICES)
choose_party = forms.ChoiceField(label = 'Select party', widget = forms.Select, choices = PARTY_CHOICES)
#views.py
def select_combo(request):
combo_form = FindCombo()
show_results = False
matches = {}
if 'choose_gender' in request.GET and 'choose_race' in request.GET and 'choose_party' in request.GET:
show_results = True
gender = request.GET['choose_gender']
race = request.GET['choose_race']
party = request.GET['choose_party']
matches = Term.objects.filter(councilperson__gender__exact=gender).filter(councilperson__race__exact=race).filter(party__exact=party)
else:
gender, race, party = "none", "none", "none"
variables = RequestContext(request, {'combo_form':combo_form, 'gender':gender, 'race':race, 'party':party, 'show_results':show_results, 'matches':matches})
return render_to_response('combo.html', variables)
#combo.html
<div id = "search-form" style = 'width:200px;'>
<form id = "search-form_box" method = "get" action = ".">
{{ combo_form.as_p }}
<input type = "submit" value = "search" />
</form>
</div> <!-- end search form div -->
<div id = 'results'>
{% if show_results %}
You selected {{ race }}, {{ gender }}, {{ party }}
{% if matches %}
<p>Found {{ matches|length }} matches:</p>
{% for m in matches %}
<p>{{ m.councilperson.first_name }} {{ m.councilperson.last_name }}, {{ m.start_date }} to {{ m.end_date }}. District: {{ m.district }} </p>
{% endfor %}
{% else %}
no matches found
{% endif %}
{% else %}select from options above
{% endif %}
</div>
我无法弄清楚两个视图/模板之间的区别以及它们的行为方式不同。有什么想法吗?
答案 0 :(得分:1)
对于您的年份表格,您有:
if query:
form = SearchFormByYear({'query':query})
使用查询的初始值创建表单。您不是为FindCombo表单执行此操作,如果要使用已选择的值创建它,则应该这样做。
类似的东西:
form = FindCombo({'choose_gender': gender, 'choose_race': race, 'choose_party': party})