How to link the Forms created in Django to the forms already created in the HTML template?

Asked

Viewed 802 times

3

I started to see about forms in Django, only I had already created in templates of my project the forms with all inputs necessary. With the creation of forms in Django there is a way for me to link each form to a form already created on template so he doesn’t create another form with more inputs?

1 answer

4


As explained in this section of the documentation, you do not need to use the automatic form generation method ({{ form.as_p }}) if you don’t want to. Just have the fields in your template input correct (with the names appropriate to each field).

A way to find out what format is expected by a Form specific is - in shell for example - call the method as_p and see what your exit is:

>>> f = ContactForm(auto_id=True)
>>> print(f.as_p())
<p><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p>
<p><label for="message">Message:</label> <input type="text" name="message" id="message" /></p>
<p><label for="sender">Sender:</label> <input type="email" name="sender" id="sender" /></p>
<p><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p>

Source

From there you can check if your manually created template is in accordance with what is expected by Form, and if you are you can use it without problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.