我有两个依赖的Django模板,也就是说,一旦我在第一个模板的表单上点击提交,我应该重定向到第二个模板中查看结果但我遇到错误:{{1}其中 tickets_per_day_results 是应显示结果的模板。
表单
NoReverseMatch at /tickets_per_day_results/
Views.py
class DropDownMenuForm(forms.Form):
week = forms.ChoiceField(choices=[(x,x) for x in range (1,53)])
year = forms.ChoiceField(choices=[(x,x) for x in range (2016,2021)])
urls.py
class ChartData8(APIView):
def get(self, request):
template_name = 'personal_website/tickets_per_day_no_results.html'
form = DropDownMenuForm()
return render(request, template_name, {'form': form})
def post(self, request):
template_name = 'personal_website/tickets_per_day_results.html'
#print(template_name)
if request.method == "POST":
year = request.POST.get('select_year', None)
week = request.POST.get('select_week', None)
... do stuff ....
return render(request, template_name, data)
模板1:tickets_per_day_no_results :这是您从下拉菜单中选择选项的第一个模板。我的猜测是对第二个模板的操作导致了问题。
url(r'^tickets_per_day_no_results/$', ChartData8.as_view()),
url(r'^tickets_per_day_results/$', ChartData8.as_view()),
模板2:tickets_per_day_results :这是您应该看到结果的第二个模板。
{% extends "personal_website/header.html"%}
{% block content %}
<h3>Please, select the year and week number to retrieve the data.</h3>
<form id="search_dates" method="POST" action="/tickets_per_day_results/"> {% csrf_token %}
<h6>Select year</h6>
<div class="row">
<div class="col-sm-8">
<select name="select_year">
<option value = {{form.year}}></option>
</select>
</div>
<div class="col-sm-8">
<h6>Select week</h6>
<select name="select_week">
<option value= {{form.week}}></option>
</select>
<button type="submit">Search</button>
</div>
</div>
</div>
如何解决此问题,以便在浏览器的第二个模板中看到结果?
答案 0 :(得分:0)
这是在某些应用程序中定义的吗?是否在表格中完全提到了网址? “/ tickets_per_day_results /”。可能是因为你错过了与app相关的url后缀,你在root urls.py中提到了这个后缀。也许“something_here / tickets_per_day_results /".