完成了modelForm的第一部分

master
RogerWork 1 year ago
parent b9942e15b6
commit 241404ca37
  1. 6
      django_demo/urls.py
  2. 38
      form_demo/forms.py
  3. 12
      form_demo/urls.py
  4. 17
      form_demo/views.py
  5. 27
      templates/form_demo/userinfomodels.html

@ -14,14 +14,12 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path
from django.urls import path, re_path, include
from django.views.static import serve
from form_demo import views
from . import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('userinfo/', views.userinfo_form),
path('uploadimg/', views.img_file_form),
re_path(r'media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}),
path('', include('form_demo.urls'))
]

@ -15,9 +15,11 @@ def mobile_validate(mobile):
raise ValidationError('手机号格式错误')
STATUS = ((None, '请选择'), (0, '正常'), (1, '无效'))
# 用户信息表单
class UserInfoForm(forms.Form):
STATUS = ((None, '请选择'), (0, '正常'), (1, '无效'))
username = forms.CharField(label="用户名", min_length=2, widget=forms.widgets.TextInput(
attrs={'class': 'form-control', 'placeholder': '请输入用户名'}),
error_messages={'required': '用户姓名不能为空', 'min_length': '长度最小6位',
@ -40,3 +42,37 @@ class ImgFileForm(forms.Form):
name = forms.CharField()
heading = forms.FileField()
class UserInfoModelForm(forms.ModelForm):
confirm_password = forms.CharField(label='确认密码', widget=forms.PasswordInput(render_value=True),
error_messages={'required': '密码不能为空'})
class Meta:
model = UserBaseInfo
fields = ['username', 'password', 'confirm_password', 'age', 'mobile', 'status']
# fields = '__all__' 使用全部字段
labels = {'age': '年龄', 'mobile': '手机号码'}
widgets = {'password': forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True),
'status': forms.widgets.Select(choices=STATUS, attrs={'selected': 0})}
error_messages = {'username': {'required': '用户名不能为空',
'min_length': '用户名至少两个字符',
'invalid': '输入正确的用户名'},
'password': {'max_length': '密码最长10位',
'min_length': '密码最少6位',
'required': '密码不能为空'},
'age': {'required': '年龄不能为空'},
'mobile': {'required': '手机号不能为空'},
'status': {'required': '用户状态不能为空'}}
def clean_mobile(self):
mobile = self.cleaned_data.get('mobile')
mobile_re = re.compile(r'^1[3-9][0-9]{9}$')
if not mobile_re.match(mobile):
raise ValidationError('手机号格式错误')
return mobile
def clean(self):
password = self.cleaned_data.get('password')
confirm_password = self.cleaned_data.get('confirm_password')
if password != confirm_password:
raise ValidationError('两次输入的密码不一致')

@ -0,0 +1,12 @@
from django.contrib import admin
from django.urls import path, re_path
from django.views.static import serve
from form_demo import views
from django_demo import settings
urlpatterns = [
path('userinfo/', views.userinfo_form),
path('uploadimg/', views.img_file_form),
path('userinfomodel/', views.userinfo_model_form),
re_path(r'media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}),
]

@ -37,3 +37,20 @@ def img_file_form(request):
print('上传成功')
print(userimg.heading)
return render(request, 'form_demo/uploadimg.html', {'form_obj': f, 'user_img': userimg})
def userinfo_model_form(request):
if request.method == 'GET':
model_form = UserInfoModelForm()
return render(request, 'form_demo/userinfomodels.html', {'form_obj': model_form})
else:
f = UserInfoModelForm(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/userinfomodels.html', {'form_obj': f, 'errors': errors})
return render(request, 'form_demo/userinfomodels.html', {'form_obj': f})

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user_info_models</title>
</head>
<body>
<form action="" method="POST" novalidate>
{% csrf_token %}
<p>{{ form_obj.username.label }}:{{ form_obj.username }}
{{ errors.username.0 }}</p>
<p>{{ form_obj.password.label }}:{{ form_obj.password }}
{{ errors.password.0 }}</p>
<p>{{ form_obj.confirm_password.label }}:{{ form_obj.confirm_password }}
{{ errors.config_password.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