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. javase 超市库存系统

    package com.oracle.demo01; import java.util.ArrayList; import java.util.Scanner; public class Demo01 ...

  2. 搭建`wenblogic`执行`install`脚本失败

    搭建weblogic服务,前期准备都已经完成,安装包都是已上传,执行install_wls1213.sh脚本,出现以下报错: install_wls1213.sh: line 1: rectory: ...

  3. js和java中URI的编码和解码

    js中对文字进行编码主要有三个函数:escape,encodeURI,encodeURIComponent: 对应解码为:unescape,decodeURI,decodeURIComponent 这 ...

  4. 第一章 JavaScript 简介

    1.1   JavaScript 的简史 JavaScript 诞生于1995年 ,后由 欧洲计算机制造商协会( ECMA,European Computer Manufacturers Associ ...

  5. Problem 14

    Problem 14 # Problem_14.py """ The following iterative sequence is defined for the se ...

  6. Myeclipse中将项目上传到码云

    公司实习之后习惯是代码上传到svn上,最近想起来个人的一些代码上传的到码云上比较方便,根据网上分享的博客内容结合自己的整理记录 其中大多数是参考了https://blog.csdn.net/izzyl ...

  7. 【codeforces 803C】Maximal GCD

    [题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...

  8. BA-Johnson楼控简介

  9. HDU 3886

    一开始又往打表想了....不过,打表确实不好处理,转DFS. DFS有几个要注意的问题,1.对于枚举以零开始的数.我纠结了很久,最终学习别人的方法,设一个BOOL,并且假设最高一位有零,很方便.2.当 ...

  10. navicate11不能激活的问题

    navicate11不能激活的问题 学习了:http://blog.csdn.net/sanbingyutuoniao123/article/details/52589678 不要安装在系统盘,如果安 ...