By default _form.as._p spits out:
<p><label for="id_subject">Subject:</label>
<input id="id_subject" type="text" name="subject" maxlength="100" /></p>
Whereas I need
<p><label for="id_subject">Subject:</label><p>
<input id="id_subject" type="text" name="subject" maxlength="100" /></p>
with a break between the label and the input. How can I modify my Django code to do so?
as_p() [Django-doc] is a method on a Form . It produces a SafeText object [Django-doc] that contains HTML code to be included in the template.
as_p }} – Render Django Forms as paragraph.
The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.
You simply just can't use form.as_p
anymore. If the defaults don't work for you, then you must render the fields manually:
<form action="/contact/" method="post">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
See the docs: https://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
If you just need a break, then there's no need to change the Django code. Just use CSS to style label
as display: block
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With