【发布时间】:2017-12-07 15:32:54
【问题描述】:
...我是 django 的新手,我遇到了这个问题:当我想显示在我的下拉列表中选择的数据时,它不起作用...这是代码
index.html
<form method="post" action="/getdata/">
<select name="Lista">
<option selected="selected" disabled>Objects on page:</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
<input type="submit" value="Select">
</form>
views.py
定义索引(请求):
if request.method == 'POST':
Lista = request.POST.get('Lista')
print Lista
context = {
Lista: 'Lista'
}
return render(request, 'showdata.html', context)
else:
template = loader.get_template('index.html')
return HttpResponse(template.render())
getdata.html
<form method="post" action="/getdata/">
<table>
<tr>
<td>Numero:</td>
<td><strong>{{ Lista }}</strong></td>
</table>
</form>
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^', views.home, name="home"),
#define the url getdata that we have written inside form
url(r'^getdata/', views.index),
# defining the view for root URL
url(r'^$', views.index),
]
当我执行服务器时,我只看到以下“Numero:”没有选择任何数字...
感谢您的帮助...
【问题讨论】:
-
请显示您的网址格式。您的表单提交到
/getdata/,但您尚未显示呈现getdata.html模板的那个 URL 的视图。 -
打印在终端中工作......我可以看到 POST 数据,但浏览器没有显示数据
-
我不使用表格......
-
我没有使用 getdata.html,也许我错了,但是 url: "getdata" 在我的views.py 文件中调用了索引函数……它调用了showdata.html ...
-
您的问题中有
getdata.html,但听起来实际上是showdata.html。
标签: django forms post dropdown