又写了一点

master
RogerWork 1 year ago
parent ae2f191f3b
commit adce8d27f7
  1. 23
      form_demo/forms.py
  2. 18
      form_demo/migrations/0002_alter_userstatus_id.py
  3. 2
      form_demo/models.py
  4. 11
      form_demo/views.py
  5. 16
      templates/form_demo/userinfo.html

@ -1,13 +1,18 @@
from django import forms
from django.core.exceptions import ValidationError
from .models import *
import re
def age_validate(age):
if age <= 0:
if age <= 0 or age > 150:
raise ValidationError('年龄错误')
elif age > 150:
raise ValidationError('老妖精')
def mobile_validate(mobile):
mobile_re = re.compile(r'^1[3-9]]\d{9}$')
if not mobile_re.match(mobile):
raise ValidationError('手机号格式错误')
# 用户信息表单
@ -19,11 +24,13 @@ class UserInfoForm(forms.Form):
'invalid': '请输入正确用户名'})
password = forms.CharField(label='密码', min_length=6, max_length=10,
widget=forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True),
help_text='密码长度为6-10个字符')
help_text='密码长度为6-10个字符', error_messages={'required': '密码不能为空'})
age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate],
error_messages={'required': '年龄不能为空'})
mobile = forms.CharField(label='手机号码', min_length=11, max_length=11)
status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select,
error_messages={'required': '状态不能为空'})
# status = forms.ModelChoiceField(queryset=UserStatus.objects.all(), empty_label='请选择')
mobile = forms.CharField(label='手机号码', validators=[mobile_validate],
error_messages={'required': '手机号不能为空'})
# status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select,
# error_messages={'required': '状态不能为空'})
status = forms.ModelChoiceField(label='状态', queryset=UserStatus.objects.all(), empty_label='请选择',
error_messages={'required': '状态不能为空'})
createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"])

@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2023-07-26 01:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('form_demo', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='userstatus',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]

@ -13,5 +13,5 @@ class UserBaseInfo(models.Model):
class UserStatus(models.Model):
id = models.AutoField(verbose_name='状态id', primary_key=True)
# id = models.AutoField(verbose_name='状态id', primary_key=True)
status = models.CharField(verbose_name='状态', max_length=10)

@ -6,3 +6,14 @@ def userinfo_form(request):
if request.method == "GET":
myform = UserInfoForm()
return render(request, "form_demo/userinfo.html", {'form_obj': myform})
else:
f = UserInfoForm(request.POST)
if f.is_valid():
print(f.clean())
print(f.cleaned_data['username'])
print(f.data)
else:
errors = f.errors
print(errors)
return render(request, "form_demo/userinfo.html", {'form_obj': f, 'errors': errors})
return render(request, "form_demo/userinfo.html", {'form_obj': f})

@ -5,8 +5,22 @@
<title>form_demo</title>
</head>
<body>
<form action="" method="POST" novalidate>
{% csrf_token %}
{{ form_obj.as_p }}
<p>{{ form_obj.username.label }}:{{ form_obj.username }}
{{ errors.username.0 }}</p>
<p>{{ form_obj.password.label }}:{{ form_obj.password }}
{{ errors.username.0 }}</p>
<p>{{ form_obj.status.label }}:{{ form_obj.status }}
{{ errors.status.0 }}</p>
<p>{{ form_obj.age.label }}:{{ form_obj.age }}
{{ errors.age.0 }}</p>
<p>{{ form_obj.mobile.label }}:{{ form_obj.mobile }}
{{ errors.mobile.0 }}</p>
{{ errors }}
<input type="submit" value="提交">
</form>
</body>
</html>
Loading…
Cancel
Save