首页 > 行业资讯 > Django教程-表单

Django教程-表单

时间:2023-07-27 来源: 浏览:

Django教程-表单

点击关注 Python架构师
Python架构师

gh_1d7504e4dee1

回复:python,领取Python面试题。分享Python教程,Python架构师教程,Python爬虫,Python编程视频,Python脚本,Pycharm教程,Python微服务架构,Python分布式架构,Pycharm注册码。

收录于合集
#django 5
#Django开源项目 15
#Django教程 14
#python web教程 23
#python教程 113
整理: python架构师

Django 提供了一个 Form 类,用于创建 HTML 表单。它描述了一种形式以及它如何工作和显示。

它类似于使用Model创建表单的ModelForm类,但它不需要Model。

表单类的每个字段都映射到 HTML 表单<input>元素,每个字段本身就是一个类,它管理表单数据并在提交表单时执行验证。

让我们看一个示例,其中我们也创建了一些字段。

from django import forms class StudentForm (forms.Form) : firstname = forms.CharField(label= "Enter first name" ,max_length= 50 ) lastname = forms.CharField(label= "Enter last name" , max_length = 100 )

 

创建一个 StudentForm,其中包含两个 CharField 类型的字段。Charfield 是一个类,用于在表单中创建 HTML 文本输入组件。

label用于设置组件的HTML标签,max_length设置输入值的长度。

点击领取Python面试题手册

Python从入门到进阶知识手册

渲染后,它会向浏览器生成以下 HTML。

< label for = "id_firstname" > Enter first name: </ label > < input type = "text" name = "firstname" required maxlength = "50" id = "id_firstname" /> < label for = "id_lastname" > Enter last name: </ label > < input type = "text" name = "lastname" required maxlength = "100" id = "id_lastname" />  

注意:Django Form 不包含 <form> 标签或提交按钮。我们必须在模板中自己提供这些内容。

下表给出了常用字段及其详细信息。

名称 HTML 输入 空值
BooleanField class BooleanField(**kwargs) 复选框输入 False
CharField class CharField(**kwargs) 文本输入 无论您提供什么作为empty_value。
ChoiceField class ChoiceField(**kwargs) 选择 ’’(空字符串)
DateField class DateField(**kwargs) 日期输入 None
DateTimeField class DateTimeField(**kwargs) 日期时间输入 None
DecimalField class DecimalField(**kwargs) 数字输入 None
EmailField class EmailField(**kwargs) 电子邮件输入 ’’(空字符串)
FileField class FileField(**kwargs) 可清除文件输入 None
ImageField class ImageField(**kwargs) 可清除文件输入 None

让我们看一下在 Django Form 类的帮助下创建 HTML 表单的完整示例。

在 Django 中构建表单

假设我们要创建一个表单来获取学生信息,请使用以下代码。

from django import forms class StudentForm (forms.Form) : firstname = forms.CharField(label= "Enter first name" ,max_length= 50 ) lastname = forms.CharField(label= "Enter last name" , max_length = 100 )

将此代码放入forms.py文件中。

在 Django 中实例化表单

现在,我们需要在views.py文件中实例化表单。请看下面的代码。

// 视图.py

from django.shortcuts import render from myapp.form import StudentForm def index (request) : student = StudentForm() return render(request, "index.html" ,{ ’form’ :student})

将表单的上下文传递到索引模板中,如下所示:

// 索引.html

<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title > Index </ title > </ head > < body > < form method = "POST" class = "post-form" > {% csrf_token %} {{ form.as_p }} < button type = "submit" class = "save btn btn-default" > Save </ button > </ form > </ body > </ html >

在 urls.py 中提供 URL

from django.contrib import admin126459646 from django.urls import path from myapp import views urlpatterns = [ path( ’admin126459646/’ , admin126459646.site.urls), path( ’index/’ , views.index), ]

运行服务器并通过localhost:8000/index在浏览器中访问表单,它将产生以下输出。

对于 <label>/<input> 对,还有其他输出选项:

  • {{ form.as_table }} 会将它们呈现为包裹在 <tr> 标签中的表格单元格

  • {{ form.as_p }} 会将它们呈现在 <p> 标签中

  • {{ form.as_ul }} 会将它们呈现在 <li> 标签中

注意:我们必须自己提供周围的 <table> 或 <ul> 元素。
 
热门推荐
  • 创世女神的艺术奇迹

  • Django教程-Django URL映射

  • “不问工资难道问候你全家”?00后面对老板的嚣张,90后甘拜下风

版权:如无特殊注明,文章转载自网络,侵权请联系cnmhg168#163.com删除!文件均为网友上传,仅供研究和学习使用,务必24小时内删除。
相关推荐