完成第一个form页面

master
RogerWork 1 year ago
parent cac3d691cf
commit ae2f191f3b
  1. 4
      django_demo/settings.py
  2. 2
      django_demo/urls.py
  3. 14
      form_demo/forms.py
  4. 7
      form_demo/views.py
  5. 12
      templates/form_demo/userinfo.html

@ -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': [

@ -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)
]

@ -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"])

@ -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})

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form_demo</title>
</head>
<body>
{% csrf_token %}
{{ form_obj.as_p }}
<input type="submit" value="提交">
</body>
</html>
Loading…
Cancel
Save