django-表单之获取表单信息(二)
urls.py
from django.urls import path from . import views urlpatterns = [ path('',views.index,name="index"), path('register/',views.IndexForms.as_view(),name='register') ]
forms.py
from django import forms class RegisterForms(forms.Form): # test=forms.Field(required=False,label='测试用',initial='请输入用户名',help_text='请输入用户名', # label_suffix='>>>') choices={ (1,'男'),(2,'女'),(3,'保密') } hobbies=[(1,'足球'),(2,'篮球'),(3,'读书')] formats=[ '%Y-%m-%d', '%m/%d/%Y', ] year_list=[ 1990,1991,1995,2001 ] username=forms.CharField(min_length=4,max_length=10,label='用户名', widget=forms.TextInput(attrs={'class':'custom-forms'})) password=forms.CharField(widget=forms.PasswordInput(attrs={'class':'custom-forms'}),min_length=4,max_length=8,label='输入密码') repassword=forms.CharField(widget=forms.PasswordInput(attrs={'class':'custom-forms'}),min_length=4,max_length=8,label='确认密码') age=forms.IntegerField(widget=forms.NumberInput(attrs={'class':'custom-forms'}),label='年龄',min_value=18,max_value=120) gender=forms.ChoiceField(choices=choices,label='性别',initial=1) # email=forms.EmailField(widget=forms.EmailInput(attrs={'class':'custom-forms'}),label='邮箱') # phone=forms.CharField(widget=forms.TextInput(attrs={'class':'custom-forms'}),max_length=11,label='电话') hobby = forms.MultipleChoiceField(label='爱好', choices=hobbies, widget=forms.CheckboxSelectMultiple) birthday=forms.DateField(label='出生日期',input_formats=formats) introduce=forms.CharField(widget=forms.Textarea(attrs={'class':'custom-forms'}),label='自我介绍')
views.py
from .forms import RegisterForms
from django.views import View
from django.http import HttpResponse
from django.shortcuts import render class IndexForms(View): def get(self,request): forms =RegisterForms() return render(request,'index.html',{'forms':forms}) def post(self,request): forms =RegisterForms(request.POST) if forms.is_valid(): username=forms.cleaned_data.get('username') password = forms.cleaned_data.get('password') repassword = forms.cleaned_data.get('repassword') age = forms.cleaned_data.get('age') gender = forms.cleaned_data.get('gender') hobby = forms.cleaned_data.get('hobby') birthday = forms.cleaned_data.get('birthday') introduce = forms.cleaned_data.get('introduce') # print([username,password,repassword,birthday,age,gender,hobby,introduce]) context={ 'datalist':{ 'username':username, 'password':password, 'repassword':repassword, 'age': age, 'gender': gender, 'hobby': hobby, 'birthday':birthday, 'introduce':introduce, } } return render(request,'home.html',context=context) else: return HttpResponse('Sorry')
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{title}}</title> <link rel="stylesheet" href={% static 'css/index.css' %}> </head> <body> <div class="content"> <!--<h1>这是主界面</h1>--> <form action="" method="post"> <table> <!--as_p,as_ul--> {{forms.as_table}} <tr> <td><input type="submit" value="submit" name="submit"></td> </tr> </table> </form> <!--{% block content %}--> <!--{% endblock %}--> </div> </body> </html>
home.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> {% for k,v in datalist.items%} <h1>{{k}}---{{v}}</h1> {% endfor %} </body> </html>
index界面:
输入相应信息,点击提交,信息会传到home界面,并显示