建立数据库
from django.db import models
# Create your models here.
class Admin(models.Model):
username=models.CharField(verbose_name='用户名',max_length=32)
password=models.CharField(verbose_name='密码',max_length=64)
class Department(models.Model):
title=models.CharField(verbose_name='部门名称',max_length=32)
def __str__(self):
return self.title
class UserInfo(models.Model):
name=models.CharField(verbose_name='姓名',max_length=16)
password=models.CharField(verbose_name='密码',max_length=64)
age=models.IntegerField(verbose_name='年龄')
account=models.DecimalField(verbose_name='账户余额',max_digits=10,decimal_places=2,default=0) #10位整数 2位小数 默认0
create_time=models.DateField(verbose_name='入职时间')
#外键 级联删除 设置空
depart=models.ForeignKey(verbose_name='部门',to='Department',to_field='id',on_delete=models.CASCADE)
# depart=models.ForeignKey(to='Department',to_field='id',on_delete=models.SET_NULL(),null=True,blank=True)
gender_chocies=(
(1,'男'),
(2,'女'),
)
gender=models.SmallIntegerField(verbose_name='性别',choices=gender_chocies)
class PrettyNum(models.Model):
mobile=models.CharField(verbose_name='手机号',max_length=11)
price=models.IntegerField(verbose_name='价格',default=0)
level_choices=(
(1,'1级'),
(2,'2级'),
(3,'3级'),
(4,'4级'),
)
level=models.SmallIntegerField(verbose_name='级别',choices=level_choices,default=2)
status_choices=(
(1,'已占用'),
(2,'未使用'),
)
status=models.SmallIntegerField(verbose_name='状态',choices=status_choices,default=1)
md5 数据校验
import hashlib
from django.conf import settings
def md5(data_string):
# 加盐
obj=hashlib.md5(settings.SECRET_KEY.encode('utf-8'))
obj.update(data_string.encode('utf-8'))
return obj.hexdigest()
form组件
from django.shortcuts import render, redirect
from app02.utils.bootstrap import BootStrapModelForm
from django import forms
from app02 import models
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
class UserModelForm(BootStrapModelForm):
# 增加输入的修改条件
# 直接重写
name = forms.CharField(min_length=3, label='用户名')
password = forms.CharField(min_length=6, label='密码')
# age=forms.CharField(min_length=3,label='用户名')
class Meta:
model = models.UserInfo
fields = ['name', 'password', 'age', 'account', 'create_time', 'gender', 'depart']
# 修改css
# widgets={
# 'name':forms.TextInput(attrs={'class':'form-control'}),
# 'password':forms.PasswordInput(attrs={'class':'form-control'}),
# 'age':forms.TextInput(attrs={'class':'form-control'}),
# 'account':forms.TextInput(attrs={'class':'form-control'}),
# 'create_time':forms.TextInput(attrs={'class':'form-control'}),
# 'gender':forms.TextInput(attrs={'class':'form-control'}),
# 'depart':forms.TextInput(attrs={'class':'form-control'}),
# }
class PrettyModelForm(BootStrapModelForm):
# 校验方式1
mobile=forms.CharField(
label='手机号',
validators=[RegexValidator(r'^1\d{10}$','手机号格式错误')],
)
# # 确保手机号格式错误,[,]中写多个RegexValidator表示格式 ,不满足条件就写参数~~错误
# # 正则表达式^表示开头 $表示结尾 \d表示整数 {10}表示10个
class Meta:
model = models.PrettyNum
fields = '__all__' # 表示所有标签
# exclude=['level'] #表示排除level标签
# def clean_mobile(self):
# txt_mobile = self.cleaned_data['mobile']
# if len(txt_mobile) != 11:
# # 格式错误时
# raise ValidationError('格式错误')
#
# # 没有错误时
# return txt_mobile
def clean_mobile(self):
txt_mobile = self.cleaned_data['mobile']
exists = models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
if exists:
# 格式错误时
raise ValidationError('号码已存在')
# 没有错误时
return txt_mobile
class PrettyEditModelForm(BootStrapModelForm):
# mobile=forms.CharField(disabled=True,label='手机号')
mobile = forms.CharField(
label='手机号',
validators=[RegexValidator(r'^1\d{10}$', '手机号格式错误')],
)
class Meta:
model = models.PrettyNum
fields = '__all__' # 表示所有标签
# exclude=['level'] #表示排除level标签
def clean_mobile(self):
# self.instance.pk 表示数据库中id
txt_mobile = self.cleaned_data['mobile']
exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
if exists:
# 格式错误时
raise ValidationError('号码已存在')
# 没有错误时
return txt_mobile
标签:web,name,models,django,forms,mobile,class,verbose
From: https://www.cnblogs.com/tgfoven/p/17452567.html