我无法获取单选按钮值。我用html编写
{% with form as f %}
<form action="{% url 'app:test' %}" method="POST">
{% csrf_token %}
<div>
{{ f.test1 }}
<label class="js--goTo" for="test1" dataGoTo="2">TEST1</label>
</div>
<button type="submit"">SEND</button>
</form>
{% endwith %}
forms.py
# -*- coding: utf-8 -*-
from django import forms
RESPONSE_CHOICES = (
('1', 'test1'),
('2', 'test2'),
)
class InputForm(forms.Form):
test1 = forms.ChoiceField(required=False,choices=RESPONSE_CHOICES, widget=forms.RadioSelect())
在views.py
中def test(request):
if request.method == "POST":
form = InputForm(data=request.POST)
if form.is_valid():
test1 = request.POST.get("test1")
print(test1)
当我选择test1的单选按钮并放置SEND按钮时,print(test1)显示为None。在这种情况下,我放置了test1,所以我真的不明白为什么test1的1没有打印在print(test1)中。在我的代码中?该如何解决?