我创建了一个包含 5 个选项的下拉菜单。当用户选择一个选项并按下提交按钮时,如何将该“选择”传输到 Django 中的后端/服务器?
After that, when the value of the selection reaches the backend, I intend to use the value to create another form. 有 5 种形式可供选择(每种选择一种),出现的形式类型将取决于原始选择的值。
这是我到目前为止所拥有的,但我收到错误消息“在 QueryDict 中找不到键‘选择’:{}”
这是 HTML 文件:
<div id = "f1" style="display:none">
<form name= "form1" action='/products/{{ action }}/' method='get'>
<p class="choice-header"> Product: </p>
<select id = "g1" title="selection" onchange = "showForm()">
<option value = "0">--</option>
<option value = "1">Form 1</option>
<option value = "2">Form 2</option>
<option value = "3">Form 3</option>
<option value = "4">Form 4</option>
<option value = "5">Form 5</option>
</select>
<div id= "b1" style="display:none">
<input class="button" type=submit value="{{ button }}">
</div>
</form>
</div>
这是 Django views.py 文件
def new2(request):
form_no = request.GET["selection"]
if form_no == 1:
form_to = Form1()
c = RequestContext(request, {
'action': 'add/2',
'button': 'Add',
'form': form_to,
})
return render_to_response('links/add.html', c)
谢谢您的帮助!