From 241404ca371184c88fc3d65d4871e38f0211f04a Mon Sep 17 00:00:00 2001 From: RogerWork Date: Fri, 28 Jul 2023 20:46:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86modelForm=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django_demo/urls.py | 6 ++-- form_demo/forms.py | 38 ++++++++++++++++++++++++- form_demo/urls.py | 12 ++++++++ form_demo/views.py | 17 +++++++++++ templates/form_demo/userinfomodels.html | 27 ++++++++++++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 form_demo/urls.py create mode 100644 templates/form_demo/userinfomodels.html diff --git a/django_demo/urls.py b/django_demo/urls.py index 79ec36e..1e365ce 100644 --- a/django_demo/urls.py +++ b/django_demo/urls.py @@ -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.*)', serve, {'document_root': settings.MEDIA_ROOT}), + path('', include('form_demo.urls')) ] diff --git a/form_demo/forms.py b/form_demo/forms.py index 3acfc29..7d790a6 100644 --- a/form_demo/forms.py +++ b/form_demo/forms.py @@ -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('两次输入的密码不一致') diff --git a/form_demo/urls.py b/form_demo/urls.py new file mode 100644 index 0000000..e9d5373 --- /dev/null +++ b/form_demo/urls.py @@ -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.*)', serve, {'document_root': settings.MEDIA_ROOT}), +] diff --git a/form_demo/views.py b/form_demo/views.py index 7489335..ed309dc 100644 --- a/form_demo/views.py +++ b/form_demo/views.py @@ -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}) diff --git a/templates/form_demo/userinfomodels.html b/templates/form_demo/userinfomodels.html new file mode 100644 index 0000000..b992d7d --- /dev/null +++ b/templates/form_demo/userinfomodels.html @@ -0,0 +1,27 @@ + + + + + user_info_models + + +
+ {% csrf_token %} +

{{ form_obj.username.label }}:{{ form_obj.username }} + {{ errors.username.0 }}

+

{{ form_obj.password.label }}:{{ form_obj.password }} + {{ errors.password.0 }}

+

{{ form_obj.confirm_password.label }}:{{ form_obj.confirm_password }} + {{ errors.config_password.0 }}

+

{{ form_obj.status.label }}:{{ form_obj.status }} + {{ errors.status.0 }}

+

{{ form_obj.age.label }}:{{ form_obj.age }} + {{ errors.age.0 }}

+

{{ form_obj.mobile.label }}:{{ form_obj.mobile }} + {{ errors.mobile.0 }}

+ {{ errors }} + +
+ + + \ No newline at end of file