Django中表单的用法深探

简介: 【转载说明:原文排版不是很好,为方便阅读,改进了排版】   django的表单设计真的很棒,涉及非常多的功能,今天介绍django较为主流的几种表单使用方法。注:本文中表单与form通用、模型与model通用。

【转载说明:原文排版不是很好,为方便阅读,改进了排版】

 

django的表单设计真的很棒,涉及非常多的功能,今天介绍django较为主流的几种表单使用方法。注:本文中表单与form通用、模型与model通用。
表单、模型的基础知识可以通过django book学习。传送门:http://djangobook.py3k.cn/2.0/

 


0、用例
我在本篇博客中,将使用同一个用例,前后会以不同的方式表示它,以此介绍不同的表单实现方法。
用例很简单,是一个叫做Server的类(大多数时候也可以理解为数据库中的表)。用例是我直接从项目中拿出来的,简化了一下。Server就是通常我们说的服务器,包括name、ip地址、端口、cpu个数、内存、状态等几个属性。

 


1、方法1 继承forms
django自带forms类。最简单的表单使用方法就是继承forms实现表单。
具体实现方法,首先引入forms

from django import forms 

 

然后编写具体的表单

class ServerForm(forms.Form): 
    user = forms.CharField(label=_("名称"), max_length=30, required=True, widget=forms.TextInput(attrs={'size': 20,})) 
    ip = forms.IPAddressField(label=_("IP地址"), max_length=20, required=True, widget=forms.TextInput(attrs={'size': 20,})) 
    port = forms.IntegerField(label=_("通信端口"), required=True, widget=forms.TextInput(attrs={'size': 20,})) 
    cpunum = forms.IntegerField(label=_("CPU个数"), required=True, widget=forms.TextInput(attrs={'size': 20,})) 
    mem = forms.IntegerField(label=_("内存"), required=True, widget=forms.TextInput(attrs={'size': 20,})) 
    state = forms.CharField(label=_("状态"), max_length=30, required=True, widget=forms.TextInput(attrs={'size': 20,}))

 

 


2、方法2 继承ModelForm
在实际开发中,一般会根据模型(model)生成表单(form),因此继承ModelForm是一种十分常见便捷的用法。
具体实现方法,分为两部分来介绍。


1)建立模型(model)
一般会建立一个叫做“models.py”的文件建立模型。在该文件中,需要引入models

from django.db import models 

 

然后编写具体的模型

class Server(models.Model): 
    name = models.CharField("名称", max_length=30, null=True, blank=True, unique=True) 
    ip = models.IPAddressField("IP地址", max_length=20,unique=True) 
    port = models.IntegerField("通信端口") 
    cpunum = models.IntegerField("CPU核数", null=True, blank=True) 
    mem = models.IntegerField("内存", null=True, blank=True) 
    state = models.CharField("状态", max_length=20, null=True, blank=True) 
def __str__(self): return self.name
class Meta: ordering = ['name','ip']

 


2)建立表单(form)
一般会建立一个叫做“form.py”的文件建立模型。在该文件中,需要引入ModelForm类和对应的模型(model)类

from django.forms import ModelForm 
from cloud.models import Server 

 

然后编写具体的表单

class ServerForm(ModelForm): 

    class Meta: 
        model = Server 

model = Server,表明这个表单继承名叫Server的模型。

 


3、方法3 继承ModelForm,限定属性
接着方法2继续思考,许多时候我们需要表单继承某一个模型,但是不需要继承其全部的属性。结合我们的用例来说,在添加Server时,我们假定我们只需要填写name、ip、port等信息,而不需要填写state。这时我们需要在继承模型Server的基础上,限定属性不包括state。
具体实现方法:

class ServerForm(ModelForm): 

    class Meta: 
        model = Server 
        fields = ('name', 'ip', 'prot', 'cpunum', 'mem')

 


4、方法4 继承ModelForm,修改属性
接着方法2、方法3继续思考,有时我们需要表单继承某一个模型,不仅需要限定其属性,还需要修改其中的某些属性。结合我们的用例来说,在添加Server时,我们假定我们只需要填写name、ip、port等信息,不需要填写state。同时,我们假定cupnum(cpu核数)不能任意填写,它只能是2、4、8、16中的一种,这就要求我们在继承模型Server的基础上,修改表单中的cpunum。
具体实现方法:

cpunum_choice = ( 
        ('', "---------"), 
        (2, "2"), 
        (4, "4"), 
        (8, "8"), 
        (16, "16"), 
) 


class ServerForm(ModelForm): 
    cpunum = forms.ChoiceField(label=_("CPU核数"),required=True, choices=cpunum_choice) 

    class Meta: 
        model = Server 
        fields = ('name', 'ip', 'prot', 'cpunum', 'mem')

 

 


5、方法5 继承ModelForm,添加表单项
接着方法2、方法3继续思考,有时我们需要表单继承某一个模型,不仅需要限定其属性,还需要添加新的表单项。结合我们的用例来说,在添加Server时,我们假定不需要填写state,除此之外还需要填写这个任务的紧急程度(将添加Server看作一个任务)。这时我们需要在继承模型Server的基础上,限定属性不包括state,然后包含一个新的选项“任务等级”。
具体实现方法:

level_choice = ( 
        ('ext', "特急"), 
        ('ugt', "紧急"), 
        ('nor', "普通"), 
) 


class ServerForm(ModelForm): 
    level = forms.ChoiceField(label=_("任务等级"),required=True, widget=forms.RadioSelect, choices=level_choice) 

    class Meta: 
        model = Server 
        fields = ('name', 'ip', 'prot', 'cpunum', 'mem') 

 

django表单设计涵盖较广,还设计widget等内容,虽与本文相关,但不是本文重点,这里不做详细阐述,有机会再写相关内容。

 

目录
相关文章
|
24天前
|
Python
Django表单
【6月更文挑战第13天】Django表单。
15 5
|
23天前
|
测试技术 数据库 Python
使用django构建表单测试
【6月更文挑战第14天】该文档介绍了如何对本地库进行自动化测试,特别是关注于代码结构和模型测试。作者鼓励为其他模型和表单创建类似的测试,并提及测试应避免对底层框架的重复验证。
55 0
使用django构建表单测试
|
1月前
|
Python
Django表单
【6月更文挑战第3天】Django表单。
14 1
|
1月前
|
Python
Django表单
【6月更文挑战第3天】Django表单。
13 1
|
11月前
|
前端开发 中间件 数据安全/隐私保护
【Django】创建用户,中间件,表单组件
【Django】创建用户,中间件,表单组件
53 0
|
2月前
|
前端开发 开发者 Python
django表单
django表单
35 0
|
7月前
|
Python
如何使用 Django Forms 创建表单?
如何使用 Django Forms 创建表单?
51 0
如何使用 Django Forms 创建表单?
|
7月前
|
JSON 安全 数据库
在 Django 中呈现 HTML 表单(GET 和 POST)
在 Django 中呈现 HTML 表单(GET 和 POST)
51 0
|
12月前
|
Python
django 表单
django 表单
31 0
|
存储 前端开发 JavaScript
Django第4步_理解form表单类与处理html的post、get
Django第4步_理解form表单类与处理html的post、get
63 0