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 : #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象 ...
随机推荐
- JS 马托货物
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如何调配? <!DOCTYPE html> <html> <head> < ...
- [总结-动态规划]经典DP状态设定和转移方程
马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...
- 【hihocoder 1032】最长回文子串
[题目链接]:http://hihocoder.com/problemset/problem/1032 [题意] [题解] 原文地址:https://segmentfault.com/a/119000 ...
- structs中通过LabelValueBean构建下拉列表
Action类中增加列表 List<LabelValueBean> list = new ArrayList<LabelValueBean>(); list.add(new L ...
- 根据BDUSS获取用户ID信息
代码在 /data/svndir/business/workroot2/app/ecom/ubec/getuser
- IOS开发教程--怎样使用点9图片
事先准备一张图片: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA== ...
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...
- audio_coding模块分析和audio_conference_mixer模块分析
audio_coding 1. 主要接口 AudioCodingModuleImpl::RegisterReceiveCodec 初始化Codec AudioCodingModul ...
- SAM学习笔记
SAM学习笔记 后缀自动机(模板)NSUBSTR(Caioj1471 || SPOJ 8222) [题意] 给出一个字符串S(S<=250000),令F(x)表示S的所有长度为x的子串中,出现次 ...
- poj--3169--Layout(简单差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9098 Accepted: 4347 Descriptio ...