Django 构建模板form表单的两种方法
作者:Caoxt 时间:2021-07-01 23:44:41
通常情况下,我们想构建一张表单时会在模板文件login.html中写入
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
</form>
下面说说我们在使用django是如何在模板文件中生成form表单:
1、自定义显示的字段:
假如我们要在模板中生成一张含有username和content的表单
在app下新建forms.py(/users/forms.py)
from django import forms
class MessageGetForm(forms.Form):
username = forms.CharField(label='用户', max_length=5) #max_length 浏览器限制用户输入的字符串长度
content = forms.CharField(label='留言', max_length=100)
在视图文件中引用上边表单字段并传递给模板(users/view.py)
from apps.users.forms import MessageGetForm
class MessageView(View):
def get(self, request, *args, **kwargs):
message = MessageGetForm()
return render(request, "message.html", {"message": message})
在模板文件调用(message.html)
<form action="" method="get">
{{ message }}
</form>
渲染后的页面效果
2、将数据表的字段映射到模板文件form表单
数据表courses_lesson字段如下
在app下新建forms.py(/users/forms.py)
#此处我们假设你的项目文件courses/models.py内含有Lesson类from apps.courses.models import Lesson
from django.forms import ModelForm
class CoursesLessonForm(ModelForm):
class Meta:
model = Lesson
fields = ['add_time', 'name', 'learn_times'] #要显示的字段
在视图文件中引用上边表单字段并传递给模板(users/view.py)
from apps.users.forms import CoursesLessonForm
class MessageView(View):
def get(self, request, *args, **kwargs):
message = CoursesLessonForm()
return render(request, "message.html", {"message": message})
在模板文件调用(message.html)
<form action="" method="get">
{{ message }}
</form>
页面效果图:
所有表单类都作为 django.forms.Form 或者 django.forms.ModelForm 的子类来创建。您可以把 ModelForm
想象成 Form
的子类。实际上 Form
和 ModelForm
从(私有) BaseForm
类继承了通用功能,但是这个实现细节不怎么重要。
来源:https://www.cnblogs.com/Caoxt/p/13111579.html
标签:Django,构建,form表单
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pycharm运行程序时,控制台输出PyDev console:starting问题
2022-09-27 14:26:32
![](https://img.aspxhome.com/file/2023/2/112792_0s.png)
用60行代码实现Python自动抢微信红包
2022-06-14 04:25:04
![](https://img.aspxhome.com/file/2023/0/90380_0s.png)
Python利用Seaborn绘制多标签的混淆矩阵
2022-12-12 17:46:22
![](https://img.aspxhome.com/file/2023/4/92434_0s.jpg)
Python分割单词和转换命名法的实现
2023-11-24 00:06:16
Python如何把不同类型数据的json序列化
2021-06-01 22:52:16
一些需要禁用的PHP危险函数(disable_functions)
2023-11-23 15:29:25
在python3.64中安装pyinstaller库的方法步骤
2022-08-12 10:27:05
![](https://img.aspxhome.com/file/2023/3/66743_0s.png)
Mysql入门系列:MySQL数据目录的位置
2008-11-24 12:59:00
![](https://img.aspxhome.com/file/UploadPic/200811/20081129183226513.jpg)
Python使用Plotly绘制常见5种动态交互式图表
2023-06-08 06:27:59
![](https://img.aspxhome.com/file/2023/1/114581_0s.gif)
浅析数据完整性问题
2007-10-07 12:44:00
三种禁用FileSystemObject组件的方法
2007-09-23 15:52:00
Django 忘记管理员或忘记管理员密码 重设登录密码的方法
2021-02-14 09:40:46
python使用matplotlib:subplot绘制多个子图的示例
2021-01-26 18:13:08
![](https://img.aspxhome.com/file/2023/6/67816_0s.png)
在SAE上部署Python的Django框架的一些问题汇总
2023-12-17 19:08:25
熵值法原理及Python实现的示例详解
2021-06-19 20:31:33
![](https://img.aspxhome.com/file/2023/0/99560_0s.png)
python实现bucket排序算法实例分析
2023-09-17 17:54:07
python使用PIL剪切和拼接图片
2022-06-26 07:41:55
![](https://img.aspxhome.com/file/2023/4/93604_0s.png)
详解四种Python中基本形态学滤波的实现
2023-05-09 15:10:09
![](https://img.aspxhome.com/file/2023/1/65531_0s.png)
python web.py启动https端口的方式
2021-10-20 11:33:44
详解Python中的__new__()方法的使用
2022-09-26 09:03:56