Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing Django _form.as_p

Tags:

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?

like image 821
user995469 Avatar asked Oct 14 '11 15:10

user995469


People also ask

What does {{ form AS_P }} do?

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.

What does form AS_P mean in django?

as_p }} – Render Django Forms as paragraph.

What is form Is_valid () in django?

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.


2 Answers

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

like image 103
Chris Pratt Avatar answered Sep 25 '22 05:09

Chris Pratt


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.

like image 7
Daniel Roseman Avatar answered Sep 23 '22 05:09

Daniel Roseman