Django 表单

Django 表单

模型的编写model.py

from django.db import models


class PersonInfo (models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    age = models.IntegerField()

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '人员信息'


class Vocation(models.Model):
    id = models.AutoField(primary_key=True)
    job = models.CharField(max_length=20)
    title = models.CharField(max_length=20)
    payment = models.IntegerField(null=True, blank=True)
    person = models.ForeignKey(PersonInfo,on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id)

    class Mate:
        verbose_name = '职业信息'

视图函数view.py

def Vo(request):
    v = VocationForm()  # 实例化对象
    return render(request, 'Vo.html', locals())

路由的设置

path('', views.Vo, name='Vo')

Vo.html页面的编写

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% if v.errors %}
<p>数据出错了。错误信息:{{ v.errors }}</p>
{% else %}
<form action="" method="post">
    {% csrf_token %}
    <table>
        {{ v.as_table }}
    </table>
    <input type="submit" value="提交">
{% endif %}
</form>
</body>
</html>

在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页