Django Forms — it is an advanced set of HTML forms that you can create with Python and that support all the features of HTML forms in Python. Rendering Django forms in a template can sometimes seem messy, but with the right knowledge of Django forms and field attributes, you can easily create a great form with all the powerful features. In this article, the form is rendered as paragraphs in the template.
{{form.as_p}} — render Django forms as paragraph
Illustration {{form.as_p}} using example. Consider a project named pythonengineering
that has an application named geeks
.
Refer to the following articles to check how to create a project and an app in Django.
Let’s create a sample Django form to display and show as an example. In geeks" forms.py
enter the following code
|
Now we need a View to render this form to a template. Let’s create a view,
|
Finally, we will create a template where we need to place the form. In templates" home.html
,
|
Here, {{form.as_p}} will display them in "p" tags. Let’s check if this works accordingly or not. Open http:// localhost: 8000 /
Let’s check the is step code, whether the form is displayed as a paragraph or not. Rendering as a paragraph means that all input fields will be wrapped in "p" tags.
Here is a demo,