0.写urls.py文件

# 导入三个模块文件
from django.contrib import admin
from django.urls import path
from app01 import views

# 设置URL路径
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index, name='index'),

]

1.写model模型

from django.db import models

class t1(models.Model):
    email = models.CharField(max_length=32)
    host = models.CharField(max_length=32)
    port = models.CharField(max_length=32)
    mobile = models.CharField(max_length=11)

2.写modelform组件

# 导入forms模块
from django import forms
# 导入models文件
from app01 import models
# 导入widgets模块
from django.forms import widgets


class T1Form(forms.ModelForm):
    # 增加的新字段(Model模型中没有的字段,本示例中就是表t1模型中没有的字段)
    username = forms.CharField(
        label='账号',
        required=True,
    )
    password = forms.CharField(
        label='密码',
        required=True,
    )
    password2 = forms.CharField(
        label='确认密码',
        required=True,
    )
    class Meta:
        # 对应的Model中的类
        model = models.t1
        # 列出所有字段
        fields = ['username', 'email', 'mobile', 'password', 'password2', 'host', 'port']
        # 排除的字段
        exclude = None
        # 帮助提示信息
        help_texts = None
        # 错误信息
        error_messages = {
            'username': '用户名不能为空',
            'email': {'required': "邮箱不能为空", },
            'host': {'required': "主机不能为空", },
        }
        # 自定义插件
        widgets = {
            "username": widgets.TextInput(),
            "email": widgets.EmailInput(),
            "host": widgets.TextInput(),
            "port": widgets.TextInput(),
            "mobile": widgets.TextInput(),
        }
        # label显示字段
        labels = {
            'email': '邮箱',
            'host': '主机',
            'port': '端口',
            'mobile': '手机',
        }

    # 为每个字段统一添加form-control属性
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for filed in self.fields.values():
            if filed not in ['host', 'port']:
                filed.widget.attrs.update({'class': 'form-control'})

3.写views视图函数

from django.shortcuts import render
from app01.forms import T1Form

def index(request):
    if request.method =='GET':
        form_obj = T1Form()
        print(form_obj)
    else:
        form_obj = T1Form(request.POST)
    return render(request, 'index.html', {'form_obj':form_obj})

4.写index.html网页

<!-- 使用时,导入bootstrap的css和js>
    <!-- 表单开始 -->
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <form action="" method="post" class="form-horizontal" novalidate>
                <!-- POST请求必须写 -->
                {% csrf_token %}
                <!-- 循环Form组件实例化对象 -->
                {% for field in form_obj %}
                    <div class="form-group">
                        <!-- 渲染label标签 -->
                        <label class="col-sm-2 control-label">{{ field.label_tag }}</label>
                        <div class="col-sm-10">
                            <!-- INPUT框 -->
                            {{ field }}
                            <!-- 渲染错误 -->
                            <span style="color: red">{{ field.errors.0 }}</span>
                        </div>
                    </div>
                {% endfor %}
                <!-- 提交按钮 -->
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div><!-- 表单开始 -->