Pyhton学习——Day58
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的更多相关文章
- Pyhton学习——Day26
#多态:多态指的是一类事物有多种形态# import abc# class Animal(metaclass = abc.ABCMeta):# 同一类事物:动物# @abc.abstractclass ...
- pyhton 学习
官方学习文档 https://docs.python.org/3/tutorial/
- 20190320_head first pyhton学习笔记之构建发布
1.把代码nester.py放入文件夹nester中,在文件夹中再新建一个setup.py文件,文件内容如下: from distutils.core import setup setup( name ...
- Pyhton学习——Day2
Python开发IDE(工具)Pycharm.eclipse1.循环while 条件 #循环体 #条件为真则执行 #条件为假则执行break用于退出所有循环continue用于退出当前循环 2.Pyc ...
- Pyhton学习——Day28
#上下文协议:文件操作时使用with执行# with open('a.txt','w',encoding='utf-8') as f1:# with语句,为了让一个对象兼容with语句,必须在这个对象 ...
- Pyhton学习——Day27
# hasattr(obj,'name')-->obj.name# getattr(obj,'name',default = 'xxx')--->obj.name# setattr(obj ...
- Pyhton学习——Day25
#面向对象的几个方法#1.静态方法@staticmethod,不能访问类属性,也不能访问实例属性,只是类的工具包#2.类方法:@classmethod,在函数属性前加上类方法,显示为(cls)代表类, ...
- Pyhton学习——Day24
# #面向对象设计:# def dog(name,gender,type):# def jiao(dog):# print('One Dog[%s],wfwfwf'%dog['name'])# def ...
- Pyhton学习——Day23
#re模块方法:findall search#findall:返回所有满足匹配条件的数值,放在列表里#search : #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象 ...
随机推荐
- 执行opatch apply 报错 OPatch failed with error code 73
.执行opatch apply 报错 OPatch failed [oracle@ora_11g 14275605]$ /opt/oracle/product/db_1/OPatch/opatch a ...
- pip是用代理
内网访问外网时,需要通过vpn访问,但是pip貌似不能是用vpn访问外网.直接度娘: windows下: set http_proxy=http://代理服务器:端口 不行可以试试下面的: set h ...
- css+div 绘制多边形
/*1.正方形*/ <div id="square"></div> #square { width: 100px; height: 100px; backg ...
- POJ3984-迷宫问题【BFS】
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- ORM对象关系型映射的用法
ORM对象关系型映射的用法 -- Django模型 1.什么是ORM关系型映射 ORM 全拼Object-Relation Mapping. 中文意为 对象-关系映射. 主要实现模型对象到关系数据库数 ...
- 【codeforces 803F】Coprime Subsequences
[题目链接]:http://codeforces.com/contest/803/problem/F [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] ...
- SpringMVC的DispatcherServlet加载过程
首先在web.xml中配置容器启动监听器,这样在容器启动后Spring会初始化一个ServletContext,负责加载springmvc的九大组件(调用DispatcherServlet.onRef ...
- C#中的IEnumerable<T>知识点
1.扩展IEnumerable<T>的方法 使继承了IEnumeralbe<T>的接口有了MyS方法 static class MySum { ...
- spring cloud 中Actuator不显示更多信息的处理方式
spring cloud 中Actuator不显示更多信息的处理方式 直接咨询了周大立,他说 management.security.enabled = false 就可以了: 学习了:http:// ...
- 写一个函数,输入int型,返回整数逆序后的字符串
刚刚看到一个面试题:写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回"321". 要求必须用递归,不能用全局变量,输入必须是一个參数.必须返回字符串.&quo ...