I try to render a simple checkbox (a boolean checkbox) in Django.
我尝试在Django中渲染一个简单的复选框(一个布尔复选框)。
form.py
form.py
class Stateform(forms.Form):
state = forms.BooleanField()
html
HTML
<form "..." method="post">{% csrf_token %}
<input type="checkbox">
<label>{{ form }}</label>
</input>
</form>
view.py
view.py
def Defstate(request, *args):
if request.method =='POST':
form = Stateform(request.POST)
if form.is_valid():
...
else :
form = Stateform()
context = {
'form': Stateform(),
}
return render(request, 'Page1/Page3.html', context)
It display the checkbox correctly but it's impossible for me to check the box...
它正确显示复选框,但我无法选中复选框...
2 个解决方案
#1
0
Use the following html code:
使用以下html代码:
<form action="..." method="post">
{{ form }}
<input type="submit" value="Submit">
</form>
Also the naming of Form
is somehow not very specific and is maybe misleading with the Form
from django.forms
which would render as empty form when used.
此外,Form的命名在某种程度上不是非常具体,可能会误用django.forms中的Form,使用时会呈现为空表单。
#2
0
I've finally found the problem!
我终于找到了问题!
When you use <label>
you must define an id and it should be same as <input>
.
当您使用
Like :
喜欢 :
<input id="id_1" type="checkbox">
<label for="id_1">
{{ form }}
</label> </input>
#1
0
Use the following html code:
使用以下html代码:
<form action="..." method="post">
{{ form }}
<input type="submit" value="Submit">
</form>
Also the naming of Form
is somehow not very specific and is maybe misleading with the Form
from django.forms
which would render as empty form when used.
此外,Form的命名在某种程度上不是非常具体,可能会误用django.forms中的Form,使用时会呈现为空表单。
#2
0
I've finally found the problem!
我终于找到了问题!
When you use <label>
you must define an id and it should be same as <input>
.
当您使用
Like :
喜欢 :
<input id="id_1" type="checkbox">
<label for="id_1">
{{ form }}
</label> </input>