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. [POI2007]MEG-Megalopolis 树状数组 + dfs序前缀和 好题

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const ...

  2. python与图灵机器人交互(ITCHAT版本)

    #!/usr/bin/env python#-*- coding:utf-8 -*- @Author : wujf @Time:2018/9/5 17:42import requestsimport ...

  3. nginx获取经过层层代理后的客户端真实IP(使用正则匹配)

    今天帮兄弟项目搞了一个获取客户端真实IP的问题,网上这种问题很多,但是对于我们的场景都不太合用,现把我的解决方案share给大家,如有问题,请及时指出. 场景: 在请求到达后端服务之前,会经过层层代理 ...

  4. 「BZOJ3339」Rmq Problem(5366)

    题目描述 输入 输出 样例输入 7 5 0 2 1 0 1 3 2 1 3 2 3 1 4 3 6 2 7 提示 这个题说来也挺有意思的 当时集训的时候遇到了一道类似的题,但是题意与此不同,我太菜了, ...

  5. 网络教程(8)IP协议

    两台计算机间不是直接连接的,所以不能使用PPP协议,那么它们是怎么链接的呢? host A想把信息传给host B ethernet帧中的目标mac地址传到SFO后开始使用PPP协议   [Media ...

  6. ASP 读取Word文档内容简单示例_组件开发_新兴网络_20161014161610.jpg

  7. obj-c部分对象快捷赋值和取值

    NSNumber: NSNumber *number = @1234; 旧的方式: NSArray *physicsValues = [NSArrayarrayWithObjects: [NSNumb ...

  8. 为什么在JavaScript中0.1+0.2不等于0.3?

    0.1+0.2不等于0.3?是不是有点颠覆你的认知,但是,在js中,是真实存在的! console.log(0.1+0.2); // 0.30000000000000004 其实这都是因为浮点数运算的 ...

  9. Python编程:从入门到实践 - matplotlib篇 - plot & scatter

    matplotlib篇 plot & scatter # filename.py 获取当前文件名方法 import sys # 当前文件名 print(sys.argv[0]) # 去除后缀后 ...

  10. 2019-03-28 git github SSH配置,上传下载操作

    1.通过git获取scrapy源码,并安装到系统里面 https://git-scm.com/download/win 下载无脑安装啊(C:\Program Files\Git),进入git bash ...