From表单验证

<!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>
<p><input type="submit" value="提交"/></p>
<p><input type="button" value="Ajax提交" onclick="submitAjaxForm();"/></p>
</form>
<script src="/static/jquery-3.1.1.js"></script>
<script>
function submitAjaxForm() {
$.ajax({
url:'/f1.html',
type:'POST',
data:$('#fm').serialize(),
success:function (arg) {
console.log(arg);
} })
}
</script>
</body>
</html>

HTML看我


from django.shortcuts import render,HttpResponse,redirect
from django import forms
from django.forms import fields
# Create your views here.
class F1Form(forms.Form):
user = fields.CharField(max_length=18,
min_length=6,
required=True,
error_messages={
'required':'用户名不能为空',
})
pwd = fields.CharField(min_length=32, required=True)
age = fields.IntegerField(
required=True,
error_messages={
'invalid':'必须为数字格式',
'required':'年龄不能为空'
}
)
email = fields.EmailField(
required=True,
error_messages={
'invalid':'必须为邮件格式,带@的那种',
'required': '邮箱不能为空'
}
) def f1(request):
if request.method =='GET':
obj = F1Form()
return render(request,'f1.html',{'obj':obj})
else:
# u = request.POST.get('user') #不能为空,长度6-18
# p = request.POST.get('pwd') #不能为空
# e = request.POST.get('email') #邮箱格式
# a = request.POST.get('age') #不能为空,必须为数字类型
#
# #1.检查是否为空
# #2.检查格式是否正确
# print(u,p,e,a)
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})

Views看我

"""day58 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from app01 import views
from app02 import views as v2
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^index.html$',views.index),
url(r'^index1.html$', views.index1),
url(r'^f1.html$', v2.f1),
]

Pyhton学习——Day58的更多相关文章

  1. Pyhton学习——Day26

    #多态:多态指的是一类事物有多种形态# import abc# class Animal(metaclass = abc.ABCMeta):# 同一类事物:动物# @abc.abstractclass ...

  2. pyhton 学习

    官方学习文档 https://docs.python.org/3/tutorial/

  3. 20190320_head first pyhton学习笔记之构建发布

    1.把代码nester.py放入文件夹nester中,在文件夹中再新建一个setup.py文件,文件内容如下: from distutils.core import setup setup( name ...

  4. Pyhton学习——Day2

    Python开发IDE(工具)Pycharm.eclipse1.循环while 条件 #循环体 #条件为真则执行 #条件为假则执行break用于退出所有循环continue用于退出当前循环 2.Pyc ...

  5. Pyhton学习——Day28

    #上下文协议:文件操作时使用with执行# with open('a.txt','w',encoding='utf-8') as f1:# with语句,为了让一个对象兼容with语句,必须在这个对象 ...

  6. Pyhton学习——Day27

    # hasattr(obj,'name')-->obj.name# getattr(obj,'name',default = 'xxx')--->obj.name# setattr(obj ...

  7. Pyhton学习——Day25

    #面向对象的几个方法#1.静态方法@staticmethod,不能访问类属性,也不能访问实例属性,只是类的工具包#2.类方法:@classmethod,在函数属性前加上类方法,显示为(cls)代表类, ...

  8. Pyhton学习——Day24

    # #面向对象设计:# def dog(name,gender,type):# def jiao(dog):# print('One Dog[%s],wfwfwf'%dog['name'])# def ...

  9. Pyhton学习——Day23

    #re模块方法:findall search#findall:返回所有满足匹配条件的数值,放在列表里#search : #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象 ...

随机推荐

  1. C++基础 (10) 第十天 C++中类型转换 异常 栈解旋 io操作

    1之前内容的回顾 C语言中的类型转换(int)a  强转可读性太差了 C++把()拆分成了四种转换方式 static_cast static_cast在编译器编译阶段就进行转换了 2.dynamic_ ...

  2. ajax异步上传文件和表单同步上传文件 的区别

    1. 用表单上传文件(以照片为例)-同步上传 html部分代码:这里请求地址index.php <!DOCTYPE html> <html lang="en"&g ...

  3. django-5-自定义模板过滤器及标签

    <<<代码布局(自定义的代码放哪里)>>> (1)某个app特有的  1.一般放app目录下 固定名为templatetags 的python文件夹里鸭,如果是别的 ...

  4. 题解 ZOJ3203 Light Bulb

    也就是loj上的#10016灯泡了... 先上原图: 因为长度肯定是个开口向下的二次函数,所以先是确定用三分来找位置,然后想办法求出当前阴影长度 看到这条斜线,就想到了一次函数,所以就建了一个系,没想 ...

  5. [HTML 5] Atomic Relevant Busy

    Together 'aria-live', we can use 'aria-atomic', 'aria-relevant' and 'aria-busy' to give more informa ...

  6. unix关于打包命令zip的使用

    unix zip命令的基本使用方法是: zip [參数] [打包后的文件名称] [打包的文件夹路径] linux zip命令參数列表: -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 - ...

  7. Android 进程常驻(5)----开机广播的简单守护以及总结

    这是一个轻量级的库,配置几行代码.就能够实如今android上实现进程常驻,也就是在系统强杀下,以及360获取root权限下.clean master获取root权限下都无法杀死进程 支持系统2.3到 ...

  8. poj 1182 食物链 &amp;&amp; nyoj 207(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52414   Accepted: 15346 Description ...

  9. java删除文件夹及子目录

    package test; import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; ...

  10. 串口之CreateFile 函数具体解释

    HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名称的指针 DWORD dwDesiredAccess, //訪问模式(写/读) DWORD dwShareM ...