从 Django 复选框中获取值
Get value from a Django checkbox
我有一个模板,我在其中创建了带有子项的不同复选框,一旦选中并提交了复选框,我希望它 return 与所有子项一起选中的项目。这是它目前的工作方式http://salty-lowlands-6607.herokuapp.com/。这是我目前在我的模板中的内容:
{% for key, value in dict.items %}
<form action="/chosen/" method="POST">
{% csrf_token %}
<ul>
<label name="choice"><li><input type="checkbox" name="checkbox" value={{ key}}>{{key}}</li>
<ol type="A">
{% for item in value %}
<li>{{item }}</li></label>
{% endfor %}
</ol>
</ul>
{% endfor %}
<input type="submit" name="submit" value="Choose Questions" >
</form>
这是我的观点:
def chosen(request):
if "checkbox" in request.POST:
message=str(request.POST.get("checkbox"))
else:
message="Nothing choosen"
return HttpResponse(message)
目前,模板中的value={{key}}只是return勾选项目的第一个词,但它旁边的{{key}}显示的是整个项目。我怎样才能让它显示整个项目和所有子项目?就像如果你选择什么是 1+1?,你会得到:
什么是 1+1?
- 1
- 2
- 3
- 4
没有问题旁边的复选框。
用引号括起变量:
value="{{ key }}"
我有一个模板,我在其中创建了带有子项的不同复选框,一旦选中并提交了复选框,我希望它 return 与所有子项一起选中的项目。这是它目前的工作方式http://salty-lowlands-6607.herokuapp.com/。这是我目前在我的模板中的内容:
{% for key, value in dict.items %}
<form action="/chosen/" method="POST">
{% csrf_token %}
<ul>
<label name="choice"><li><input type="checkbox" name="checkbox" value={{ key}}>{{key}}</li>
<ol type="A">
{% for item in value %}
<li>{{item }}</li></label>
{% endfor %}
</ol>
</ul>
{% endfor %}
<input type="submit" name="submit" value="Choose Questions" >
</form>
这是我的观点:
def chosen(request):
if "checkbox" in request.POST:
message=str(request.POST.get("checkbox"))
else:
message="Nothing choosen"
return HttpResponse(message)
目前,模板中的value={{key}}只是return勾选项目的第一个词,但它旁边的{{key}}显示的是整个项目。我怎样才能让它显示整个项目和所有子项目?就像如果你选择什么是 1+1?,你会得到:
什么是 1+1?
- 1
- 2
- 3
- 4
没有问题旁边的复选框。
用引号括起变量:
value="{{ key }}"