diff --git a/django_demo/settings.py b/django_demo/settings.py index f06e23b..2534981 100644 --- a/django_demo/settings.py +++ b/django_demo/settings.py @@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.1/ref/settings/ """ - +import os.path from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -56,7 +56,7 @@ ROOT_URLCONF = 'django_demo.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [os.path.join(BASE_DIR, "templates/")], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/django_demo/urls.py b/django_demo/urls.py index 7e15737..24f7cb3 100644 --- a/django_demo/urls.py +++ b/django_demo/urls.py @@ -15,7 +15,9 @@ Including another URLconf """ from django.contrib import admin from django.urls import path +from form_demo import views urlpatterns = [ path('admin/', admin.site.urls), + path('userinfo/', views.userinfo_form) ] diff --git a/form_demo/forms.py b/form_demo/forms.py index cbb1a2d..4739788 100644 --- a/form_demo/forms.py +++ b/form_demo/forms.py @@ -1,6 +1,6 @@ from django import forms from django.core.exceptions import ValidationError -from models import * +from .models import * def age_validate(age): @@ -13,13 +13,17 @@ def age_validate(age): # 用户信息表单 class UserInfoForm(forms.Form): STATUS = ((None, '请选择'), (0, '正常'), (1, '无效')) - username = forms.CharField(label="用户名", min_length=6, widget=forms.widgets.TextInput( - attrs={'class': 'form-control', 'placeholder': '请输入用户名'})) + username = forms.CharField(label="用户名", min_length=2, widget=forms.widgets.TextInput( + attrs={'class': 'form-control', 'placeholder': '请输入用户名'}), + error_messages={'required': '用户姓名不能为空', 'min_length': '长度最小6位', + '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个字符') - age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate]) + 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) + status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select, + error_messages={'required': '状态不能为空'}) # status = forms.ModelChoiceField(queryset=UserStatus.objects.all(), empty_label='请选择') createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"]) diff --git a/form_demo/views.py b/form_demo/views.py index 91ea44a..5d4b61a 100644 --- a/form_demo/views.py +++ b/form_demo/views.py @@ -1,3 +1,8 @@ from django.shortcuts import render +from .forms import * -# Create your views here. + +def userinfo_form(request): + if request.method == "GET": + myform = UserInfoForm() + return render(request, "form_demo/userinfo.html", {'form_obj': myform}) diff --git a/templates/form_demo/userinfo.html b/templates/form_demo/userinfo.html new file mode 100644 index 0000000..483b5cb --- /dev/null +++ b/templates/form_demo/userinfo.html @@ -0,0 +1,12 @@ + + + + + form_demo + + + {% csrf_token %} + {{ form_obj.as_p }} + + + \ No newline at end of file