I’m attempting to associate a checkbox with an input field (to indicate that the field should be updated in the model). Apparently a BooleanField should produce a checkbox in the html, but it doesn’t; it isn’t displayed.
forms.py:
class ArtworkMultiEditForm(forms.Form):
date = forms.CharField(label="Date", required=False,
widget=forms.TextInput(attrs={'class': 'text_input'}))
date_checked = forms.BooleanField()
footer.html:
<form method="POST" class="ArtworkMultiEditForm" novalidate>
{% csrf_token %}
<table>
<tr>
<td>{{form.date_checked}} Date {{form.date}}</td>
</tr>
</table>```
I cannot recreate the symptoms you are describing from the information provided here.
If you have invalid HTML being generated, browsers can “reorganize” or otherwise alter the HTML being received such that what you see in your browser’s developer tools is not the same as what was sent by the server. So one thing to check is to ensure that the complete page consists of valid HTML.
If you are looking for assistance with validating the rendered page, please post the complete template(s) being rendered that include this form. (This means showing any templates being extended or included as part of this page.) Also post the view that is rendering this form.
Thanks Ken. I don’t think the html is a problem. I tried this:
index.html:
<html>
<body>
<form method="POST" class="TestForm" >
{% csrf_token %}
<p style="padding: 10px; border: 1px solid black;"> {{form.date_checked}} </p>
<p>{{form.date}}</p>
</form>
</body>
</html>
forms.py:
class TestForm(forms.Form):
date = forms.CharField(max_length=24)
date_checked = forms.BooleanField()
in Safari console:
I put a border around the supposed checkbox to ensure it’s being rendered.
Note that it’s rendered as type=“hidden”.
That’s your csrf token - that’s always rendered as hidden.
What you want to look at next is the network tab and examine the response being sent by the request. Verify whether you’re getting the complete template from the server or not. This will help determine the location of the issue.
Also post the complete view being used here.
It might also be worth seeing what happens if you render the form simply as {{ form }}
I figured it out. My view was sending the wrong form to the template. Oof. Thanks for the help.