我想做的是,如果用户年满18岁,则请他填写表格...如果年满18岁,则请他填写其他表格并将该表格保存到数据库中>
views.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})
models.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
forms.py
from app1.models import MyUser
from django import forms
class UserForm(forms.ModelForm):
class Meta():
model=MyUser
fields='__all__'
答案 0 :(得分:0)
一种选择是解析表单数据,然后根据解析后的数据执行操作。
这应该类似于以下内容:
if form.is_valid():
age = form.cleaned_data['age']
if age < 18:
# render < 18 form
else:
# render >= 18 form
答案 1 :(得分:-1)
一种更清洁的方法是使用javascript处理它。 here's an answer关于使用js渲染Django表单