Django forms 主要的标签介绍
程序员文章站
2022-06-22 09:22:40
修改 forms.py 修改 urls.py 修改 views.py 修改 detail.html ......
修改 forms.py
from django import forms as dforms from django.forms import fields from django.forms import widgets class detailform(dforms.form): user1 = fields.charfield(widget=widgets.passwordinput) user2 = fields.charfield(widget=widgets.textinput(attrs={'class': 'c1', 'placeholder': '用户名'})) user3 = fields.choicefield(choices=[(1, 'sh'), (2, 'bj'), ]) user4 = fields.integerfield() user5 = fields.charfield( widget=widgets.select(choices=[(1, 'sh'), (2, 'bj'),]) ) user6 = fields.integerfield( widget=widgets.select(choices=[(1, 'sh'), (2, 'bj'), ]) ) user7 = fields.integerfield( widget=widgets.radioselect(choices=[(1, 'sh'), (2, 'bj'), ]) )
修改 urls.py
path('detail.html', views.detail),
修改 views.py
from django.shortcuts import render, httpresponse from django import forms from app01 import forms def detail(request): obj = forms.detailform(request.post) obj.is_valid() print(obj.clean()) return render(request, 'detail.html', {'obj': obj})
修改 detail.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> </head> <body> <form action="detail.html" method="post"> <div>user1:{{ obj.user1 }}</div> <div>user2:{{ obj.user2 }}</div> <div>user3:{{ obj.user3 }}</div> <div>user4:{{ obj.user4 }}</div> <div>user5:{{ obj.user5 }}</div> <div>user6:{{ obj.user6 }}</div> <div>user7:{{ obj.user7 }}</div> <input type="submit" value="提交"> </form> </body> </html>