zoukankan      html  css  js  c++  java
  • Django入门--简单的form验证

    url

    from django.contrib import admin
    from  app01 import views
    from django.conf.urls import url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login/',views.login)
    ]

    html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form method="POST" action="/login/">
            {% csrf_token %}
            <p>
                用户:<input type="text" name="username" />{{ obj.errors.username.0 }}
                </p>
            <p>
                密码:<input type="password" name="password" />{{ obj.errors.password.0 }}
            </p>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>
    View Code

    view

    from django.shortcuts import render,HttpResponse,redirect
    
    # Create your views here.
    
    
    from django.forms import Form
    from django.forms import fields
    class LoginForm(Form):
        #长度,是否为空的验证,正则验证:不能为空,长度6-18
        username = fields.CharField(max_length=18,
                                    min_length=6,
                                    required=True,
                                    error_messages={
                                        'required':'用户名不能为空',
                                        'max_length':'用户名太长',
                                        'min_length':'用户名太短',
                                    }
                                    )
        #长度,是否为空的验证,正则验证:不能为空,长度大于5
        password = fields.CharField(min_length=5,required=True)
    
    def login(request):
        if request.method == "GET":
            return render(request,'login.html')
        else:
            obj = LoginForm(request.POST)
            if obj.is_valid():
                print(obj.cleaned_data) ###字典类型
                return redirect('https://www.baidu.com')
            else:
                return render(request,'login.html',{'obj':obj})
    View Code
  • 相关阅读:
    vpp l3 bvi
    set interface ip address: failed to add 1 on loop1 which conflicts with
    Failed: no source address for egress interface
    vpp vrf
    vpp bvi
    creates 2 connected namespaces vpp1 & vpp2
    unknown input `arp'
    vpp cmd
    vxlan bum
    Go流程结构(if)
  • 原文地址:https://www.cnblogs.com/workherd/p/13699304.html
Copyright © 2011-2022 走看看