zoukankan      html  css  js  c++  java
  • Form组件

    form组件判断提交数据:

    >>>views
    from django import forms
    from django.forms import fields
    class Form1(forms.Form):
        user = fields.CharField(max_length=18,
                                min_length=6,
                                required=True,
                                error_messages={
                                    'required':'用户名不能为空',
                                    'max_length':'用户名不能18位',
                                    'min_length': '用户名不能小于6位',
                                }
                     ) pwd
    = fields.CharField(min_length=6, required=True, error_messages={ 'required': '密码不能为空', 'min_length': '密码不能小于6位', } ) age = fields.IntegerField(required=True, error_messages={ 'required': '年龄不能为空', 'invalid':'格式错误' } ) email = fields.EmailField(required=True, error_messages={ 'required': '邮箱不能为空', 'invalid': '格式错误' } ) def index(request): if request.method == 'GET': #get方式访问生成input 传到前端页面 form_obj = Form1() return render(request,'index.html',locals()) else: form_obj = Form1(request.POST) if form_obj.is_valid(): #验证成功的数据 form_obj.cleaned_data print("验证成功",form_obj.cleaned_data) #成功后跳转页面 return HttpResponse("ok") else: #验证失败的数据 form_obj.errors print("验证失败",form_obj.errors) return render(request,'index.html',locals()) >>>html
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <form id="fm" action="/index/" method="POST" novalidate>
    <p>用户:{{ form_obj.user }}{{ form_obj.errors.user.0 }}</p>
    <p>密码:{{ form_obj.pwd }}{{ form_obj.errors.pwd.0 }}</p>
    <p>邮箱:{{ form_obj.email }}{{ form_obj.errors.email.0 }}</p>
    <p>年龄:{{ form_obj.age }}{{ form_obj.errors.age.0 }}</p>
    <input type="submit" value="提交">
    </form>

    </body>
    </html>

     form组件

    注意:urls里面匹配正则的写法
    from django.urls import re_path        导入模块
    re_path('edit_user-(d+)/', views.edit_user)
    
    >>>models
    class Useraccount(models.Model):
        username = models.CharField(max_length=32)
        email = models.EmailField(max_length=32)
    
    >>>views
    from django import forms
    from django.forms import fields
    class UserForm(forms.Form):
        username = fields.CharField()
        email = fields.EmailField()
    def index(request):
        user_list = Useraccount.objects.all()
        return render(request,'index.html',locals())
    
    def add_user(request):
        if request.method == "GET":
            obj = UserForm()
            return render(request,'add_user.html',locals())
        else:
            obj = UserForm(request.POST)
            if obj.is_valid():
                print(obj.cleaned_data)
                Useraccount.objects.create(**obj.cleaned_data)
                return redirect('/index/')
    
            else:
                return render(request,'add_user.html',locals())
            
    def edit_user(request,nid):
        if request.method == "GET":
            data = Useraccount.objects.filter(id=nid).first()
            obj = UserForm({'username':data.username,'email':data.email})
            return render(request,'edit_user.html',locals())
        else:
            obj = UserForm(request.POST)
            if obj.is_valid():
                Useraccount.objects.filter(id=nid).update(**obj.cleaned_data)
                return redirect('/index/')
            else:
                return render(request, 'edit_user.html', locals())
    
    >>>html
    ****(index.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <a href="/add_user/">添加</a>
    <ul>
        {% for row in user_list %}
            <li>{{ row.id  }}-{{ row.username }}-{{ row.email }}<a href="/edit_user-{{ row.id }}/">编辑</a></li>
        {% endfor %}
    </ul>
    </body>
    </html>
    
    ****(add_user.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="/add_user/" method="post" novalidate>
        <p>用户名:{{ obj.username }}{{ obj.errors.username.0 }}</p>
        <p>邮箱:{{ obj.email }}{{ obj.errors.email.0 }}</p>
        <input type="submit" value="提交">
    </form>
    
    </body>
    </html>
    
    ****(edit_user.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="/edit_user-{{ nid }}/" method="post" novalidate>
        <p>用户名:{{ obj.username }}{{ obj.errors.username.0 }}</p>
        <p>邮箱:{{ obj.email }}{{ obj.errors.email.0 }}</p>
        <input type="submit" value="提交">
    </form>
    
    </body>
    </html>
  • 相关阅读:
    学Java需要了解的linux系统和unix系统
    51Nod 1649: 齐头并进(最短路)
    51Nod 1694: 两条路径(图论)
    The Preliminary Contest for ICPC Asia Shanghai 2019
    牛客练习赛52 A:数数(快速幂)
    The Preliminary Contest for ICPC Asia Shenyang 2019
    The 2019 Asia Nanchang First Round Online Programming Contest
    2019 年百度之星·程序设计大赛
    2019中国大学生程序设计竞赛(CCPC)
    2019 年百度之星·程序设计大赛
  • 原文地址:https://www.cnblogs.com/yzcstart/p/10718089.html
Copyright © 2011-2022 走看看