{{ form.as_ul }} – Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent Form with all powerful features. In this article, Form is rendered as list in the template.
{{ form.as_ul }} – Render Django Forms as list
Illustration of {{ form.as_ul }} using an Example. Consider a project named geeksforgeeks having an app named geeks.
Let’s create a sample Django Form to render it and show as an example. In geeks > forms.py, enter following code
- Python3
from django import forms # creating a form class InputForm(forms.Form): first_name = forms.CharField(max_length = 200 ) last_name = forms.CharField(max_length = 200 ) roll_number = forms.IntegerField( help_text = "Enter 6 digit roll number" ) password = forms.CharField(widget = forms.PasswordInput()) |
Now we need a View to render this form into a template. Let’s create a view,
- Python3
from django.shortcuts import render from .forms import InputForm # Create your views here. def home_view(request): context = {} context[ 'form' ] = InputForm() return render(request, "home.html", context) |
Finally, we will create the template where we need the form to be placed. In templates > home.html,
- html
< form action = "" method = "post"> {% csrf_token %} < ul > {{ form.as_ul }} </ ul > < input type="submit" value="Submit"> </ form > |
Here {{ form.as_ul }} will render them as list cells wrapped in <li> tags. Let’s check whether this is working accordingly or not. Open http://localhost:8000/ Let’s check the source code whether the form is rendered as a list or not. By rendering as a list it is meant ,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2020-05-27 ssh命令带密码
2018-05-27 如何在 Linux 中找到你的 公网IP 地址
2017-05-27 二层设备与三层设备的区别--总结