我想要做的是,如果用户在 18 岁以上,那么请他填写表格……如果低于 18 岁,那么请他填写不同的表格并将表格保存到数据库中
视图.py
from django.shortcuts import render
from app1.form import UserForm
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,"home.html")
def formpage(request):
form=UserForm()
if(request.method=='POST'):
form=UserForm(request.POST)
if(form.is_valid()):
form.save()
return home(request)
return render(request,'formpage.html',{'form':form})
模型.py
from django.db import models
# Create your models here.
gen_opts= [
('male','Male'),
('female', 'Female'),
]
class MyUser(models.Model):
name=models.CharField(max_length=40)
gender = models.CharField(max_length=6, choices=gen_opts)
age= models.PositiveIntegerField()
ph.num=models.PositiveIntegerField()
check_box = models.BooleanField()
def __str__(self):
return self.name
表格.py
from app1.models import MyUser
from django import forms
class UserForm(forms.ModelForm):
class Meta():
model=MyUser
fields='__all__'
相关分类