zoukankan      html  css  js  c++  java
  • django from表单验证

    django from表单验证

     

    实现:表单验证

    工程示例:

    urls.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
     
     
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^f1.html$', views.f1),
    ]

    settings.py

    STATIC_FILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
    )

    views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    from django.shortcuts import render
    from django.shortcuts import redirect
    from django.shortcuts import HttpResponse
    from app01 import models
     
    from django import forms
    from django.forms import fields
     
     
    class F1Form(forms.Form):
        user = fields.CharField(
            max_length=18,
            min_length=6,
            required=True,
            error_messages={'required': '用户名不能为空',
                            'max_length': '太长了',
                            'min_length': '太短了'
                            }
        )
     
        pwd = fields.CharField(
            required=True,
            min_length=32
        )
     
        age = fields.IntegerField(
            required=True,
            error_messages={
                'required': '邮箱不能为空',
                'invalid': '邮箱格式错误',
            }
        )
     
        email = fields.EmailField(
            required=True,
            min_length=8
        )
     
     
    def f1(request):
        if request.method == 'GET':
            obj = F1Form()
            return render(request, 'f1.html', {'obj': obj})
        else:
            obj = F1Form(request.POST)
            # 是否全部验证成功
            if obj.is_valid():
                # 用户提交的数据
                print('验证成功', obj.cleaned_data)
                return redirect('http://www.baidu.com')
            else:
                print('验证失败', obj.errors)
                return render(request, 'f1.html', {'obj': obj})

    f1.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form id="fm" action="/f1.html" method="POST">
        <p>用户{{ obj.user }}{{ obj.errors.user.0 }}</p>
        <p>密码{{ obj.pwd }}{{ obj.errors.pwd.0 }}</p>
        <p>年龄{{ obj.age }}{{ obj.errors.age.0 }}</p>
        <p>邮箱{{ obj.email }}{{ obj.errors.email.0 }}</p>
        <input type="submit" value="提交" />
    </form>
    <script src="/static/js/jquery-3.1.1.js"></script>
     
    </body>
    </html>
  • 相关阅读:
    力扣Leetcode 3. 无重复字符的最长子串
    力扣Leetcode 21. 合并两个有序链表
    力扣Leetcode 202. 快乐数 -快慢指针 快乐就完事了
    力扣Leetcode 面试题56
    力扣Leetcode 33. 搜索旋转排序数组
    力扣Leetcode 46. 全排列
    python123期末四题编程题 -无空隙回声输出-文件关键行数-字典翻转输出-《沉默的羔羊》之最多单词
    深入理解ReentrantLock的实现原理
    深入图解AQS实现原理和源码分析
    Java:CAS(乐观锁)
  • 原文地址:https://www.cnblogs.com/zjltt/p/7526006.html
Copyright © 2011-2022 走看看