欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django forms 主要的标签介绍

程序员文章站 2022-03-11 08:34:16
修改 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>

Django forms 主要的标签介绍
Django forms 主要的标签介绍