在我的django应用程序中,我需要使用名为'year'的参数调用视图。然后在模板中,我使用年份名称列表创建了一个表单和一个下拉列表。此时,我对如何调用视图感到困惑。
该视图名为'create_report_for_data_of_the_year'。它需要一年的参数。 即,
http://127.0.0.1:8000/myapp/reports/2011
我尝试编写如下所示的模板。
<li id="yearlydataplots" class="report">
<form action="create_report_for_data_of_the_year" >
<select name="year" id="year">
{% for anyear in years %}
<option value={{anyear}} > {{anyear}}</option>
{% endfor %}
</select>
<input type="submit" value="Plot for entries of the year"/>
</form>
</li>
但是,单击提交按钮时,浏览器会转到
http://127.0.0.1:8000/myapp/reports/create_report_for_data_of_the_year?year=2006
导致404。
我更改了method =“post”,点击提交进入
http://127.0.0.1:8000/myapp/reports/create_report_for_data_of_the_year
再次导致404
我知道,我遗漏了一些非常基本的东西:-) ..如果有人能够指出它,那就太好了
提前感谢,
标记
def create_report_for_data_of_the_year(request,year,page_title,template_name):
dataset=MyDataModel.objects.filter(today__year=year,creator=request.user)
#today is a field in MyDataModel and is a datetime.datetime
map = create_map_of_names_and_values(dataset)
basefilename = "plotofdataforyear%s"%year
page_title = page_title+" "+year
imgfilename= create_plot(map,basefilename)
report_data={'basefilename':basefilename,'plot_image':imgfilename,'year':year,'page_title':page_title}
report_data["name_value_dict"]=map
req_context=RequestContext(request,context)
return render_to_response(template_name,req_context)
和url映射是
...
url(r'^reports/(?P<year>\d{4})/$','myapp.views.create_report_for_data_of_the_year',
{
'template_name':'myapp/report_for_data_of_the_year.html',
'page_title':'report for data in the Year'
},name='report_data_for_year' ),
...
答案 0 :(得分:2)
您需要使用JS重定向到所需的页面。或者您可以使用year = request.GET.get('year')
接受一年作为参数。