![]() |
VOOZH | about |
The {{ form.as_ul }} method renders Django form fields as HTML list items. Each form field is wrapped inside an <li> element, making it suitable for forms that need to be displayed as an unordered list. This approach allows forms to be rendered with minimal template code while maintaining a structured layout.
Example:
<form method="post">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
</form>
Example: Create a project named geeksforgeeks having an app named geeks and create a sample Django Form to render it.
In geeks /forms.py,
In views.py,
In templates /home.html,
Open http://localhost:8000/ 👁 python-django-form-as-ul
Let's check the source code whether the form is rendered as a list or not. By rendering as a list it is meant that all input fields will be enclosed in <li> tags. Here is the demonstration, 👁 Image
Explanation:
Note: {{ form.as_ul }} only affects how form fields are rendered in HTML. It does not change form validation, submission handling, or database operations.
| Method | Output |
|---|---|
{{ form.as_ul }} | Renders fields inside <li> tags |
{{ form.as_p }} | Renders fields inside <p> tags |
{{ form.as_table }} | Renders fields inside table rows (<tr>) |
How to Create a Basic Project using MVT in Django?
How to Create an App in Django ?
{{ form.as_table }}
{{ form.as_p }}