Django基础五之Ajax-一品网
Django基础五之Ajax
Django基础五之Ajax
目录
Django基础五之Ajax1. Ajax介绍 2. Ajax前后端传值2.2 方法二使用Json格式返回 2.3 Ajax 发送POST请求 3. Ajax上传文件 4. Ajax提交Json格式 5. Django内置序列化 6. Ajax结合layer弹窗实现二次确认 7. 分页
1. Ajax介绍
2. Ajax前后端传值
在输入框一和输入框二中分别输入一个数字,然后点提交在第三个输入框中显示结果.
HTML代码:
2.1 方法一HttpResponse直接返回
html页面代码:
views.py代码
from django.shortcuts import render,HttpResponse
def index(request):
if request.method == 'POST': # if request.is_ajax(): 判断是不是ajax请求
n1 = request.POST.get('num1')
n2 = request.POST.get('num2')
n3 = int(n1) + int(n2)
return HttpResponse(n3)
return render(request, 'index.html')
2.2 方法二使用Json格式返回
上面例子返回的是个数字,如果返回的是多个值,或者是不同的类型,那么就要用json格式传输
2.2.1 HttpResponse 返回json类型
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
#if request.method == 'POST':
if request.is_ajax():
n1 = request.POST.get('num1')
n2 = request.POST.get('num2')
n3 = int(n1) + int(n2)
res_dict = {'username':'Hans', 'n3':n3}
import json
# 序列化成json类型的字符串
res_dict = json.dumps(res_dict)
return HttpResponse(res_dict)
return render(request, 'index.html')
前端处理Json
2.2.2 JsonResponse返回
views.py
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
# Create your views here.
def index(request):
if request.is_ajax():
n1 = request.POST.get('num1')
n2 = request.POST.get('num2')
n3 = int(n1) + int(n2)
res_dict = {'username':'Hans', 'n3':n3}
return JsonResponse(res_dict)
return render(request, 'index.html')
前端处理
使用JsonResponse返回的时候,它返回kwargs.setdefault('content_type', 'application/json') 直接返回的为application/json,ajax可以直接转成对象类型.
2.2 方法三使用HttpResponse 返回,但前端不做反序列化
views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
#if request.method == 'POST':
if request.is_ajax():
n1 = request.POST.get('num1')
n2 = request.POST.get('num2')
n3 = int(n1) + int(n2)
res_dict = {'username':'Hans', 'n3':n3}
print(res_dict)
import json
res_dict = json.dumps(res_dict)
return HttpResponse(res_dict)
return render(request, 'index.html')
前端处理
2.3 Ajax 发送POST请求
1. 在和form表单一块用的时候,form表单里不能写button 和input type="submit",因为在form表单里都是提交,和ajax一块用时form提交一次,ajax再提交一次,就会有问题。所以在form表单中使用 , 要么不用form表单
示例: 利用数据库验证登录用户名和密码,成功跳转到别的网站上,不成功提示验证失败:
HTML代码:
后端代码:
views.py
def login(request):
if request.is_ajax():
name = request.POST.get('username')
pwd = request.POST.get('password')
from lib01 import models
res = models.UserVerfiy.objects.filter(username=name, password=pwd)
print(res)
data_dict = {'status':100, 'msg':None}
if res:
data_dict['msg']="验证成功"
else:
data_dict['status']=101
data_dict['msg'] = "验证失败"
return JsonResponse(data_dict)
return render(request,"login.html")
3. Ajax上传文件
HTTP的POST请求有三种编码格式:
urlencoded: 默认 可以使用request.POST取值
form-data: 上传文件,可以使用request.POST取值
json: ajax发送json格式数据,不可以使用request.POST取值
使用Ajax和form表单,默认都是urlencoded格式
如果上传文件: form表单指定格式(enctype="multipart/form-data")
如果编码方式为urlencoded格式,在body中格式:
user=xxx&password=xxx
使用Ajax上传文件:
views.py
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
def index(request):
if request.is_ajax():
myfile = request.FILES.get('myfile')
print(type(myfile))
with open(myfile.name, 'wb') as f:
for i in myfile:
f.write(i)
return HttpResponse("上传成功")
return render(request, 'index.html')
4. Ajax提交Json格式
使用ajax提交json格式的数据,要指定编码格式:
contentType:'application/json'
在前端指定编码格式,后端收到后要反序列出来,才能使用。
$.ajax({
url:"",
type:"post",
contentType:'application/json', // 指定编码,才能向后端发json格式
//序列化成json格式
filedata = JSON.stringify(name:$('#id1').val(),password:$('id2').val())
data: filedata,
success:function (data){
console.log(data)
}
})
views.py
后端处理,拿到数据后要先反序列化:
import json
# request.get默认是取不出来json格式的。使用request.body来取出:
filedata = request.body
file_json = json.loads(filedata)
name = file_json.get('name')
password = file_json.get('password')
retrun HttpsRespone("OK")
5. Django内置序列化
5.1 内置序列化
把对象转成json格式字符串,目前要做序列化要自己循环,然后拼一个字典,再去序列化成json格式
from django.core import serializers
def book_list(request):
book_list=models.Book.objects.all()
res = serializers.serialize('json', book_list)
return HttpResponse(res)
5.2 批量插入数据
def add_book():
book_list =[]
for i in range(1000):
book=models.Books(name="图书名%s"%i, price="价格%s" %i)
book_list.append(book)
models.Books.objects.bulk_create(book_list,batch_size=100)
# batch_size 每次插入多少
使用faker模块生成测试数据:
pip3 install faker
>>> from faker import Faker
>>> faker = Faker('zh_CN')
>>> print(faker.name())
田岩
>>> print(faker.name())
黄柳
>>> print(faker.address())
云南省婷县锡山汕尾街V座 503156
>>> print(faker.phone_number())
13996473292
>>> print(faker.phone_number())
18959779669
6. Ajax结合layer弹窗实现二次确认
6.1 ajax常用参数
1.url:
要求为String类型的参数,(默认为当前页地址)发送请求的地址。
2.type:
要求为String类型的参数,请求方式(post或get)默认为get。注意其他http请求方法,例如put和delete也可以使用,但仅部分浏览器支持。
3.timeout:
要求为Number类型的参数,设置请求超时时间(毫秒)。此设置将覆盖$.ajaxSetup()方法的全局设置。
4.async:
要求为Boolean类型的参数,默认设置为true,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为false。注意,同步请求将锁住浏览器,用户其他操作必须等待请求完成才可以执行。
5.cache:
要求为Boolean类型的参数,默认为true(当dataType为script时,默认为false),设置为false将不会从浏览器缓存中加载请求信息。
6.data:
要求为Object或String类型的参数,发送到服务器的数据。如果已经不是字符串,将自动转换为字符串格式。get请求中将附加在url后。防止这种自动转换,可以查看 processData(防止自动转换)选项。对象必须为key/value格式,例如{foo1:"bar1",foo2:"bar2"}转换为&foo1=bar1&foo2=bar2。如果是数组,JQuery将自动为不同值对应同一个名称。例如{foo:["bar1","bar2"]}转换为&foo=bar1&foo=bar2。
7.dataType:
要求为String类型的参数,预期服务器返回的数据类型。如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并作为回调函数参数传递。可用的类型如下:
xml:返回XML文档,可用JQuery处理。
html:返回纯文本HTML信息;包含的script标签会在插入DOM时执行。
script:返回纯文本JavaScript代码。不会自动缓存结果。除非设置了cache参数。注意在远程请求时(不在同一个域下),所有post请求都将转为get请求。
json:返回JSON数据。
jsonp:JSONP格式。使用SONP形式调用函数时,例如myurl?callback=?,JQuery将自动替换后一个“?”为正确的函数名,以执行回调函数。
text:返回纯文本字符串。
8.beforeSend:
这个参数主要是为了在向服务器发送请求前,执行一些操作。要求为Function类型的参数,发送请求前可以修改XMLHttpRequest对象的函数,例如添加自定义HTTP头。在beforeSend中如果返回false可以取消本次ajax请求。XMLHttpRequest对象是惟一的参数。
function(XMLHttpRequest){undefined
this; //调用本次ajax请求时传递的options参数
}
9.complete:
要求为Function类型的参数,请求完成后调用的回调函数(请求成功或失败时均调用)。参数:XMLHttpRequest对象和一个描述成功请求类型的字符串。
function(XMLHttpRequest, textStatus){undefined
this; //调用本次ajax请求时传递的options参数
}
10.success:
要求为Function类型的参数,请求成功后调用的回调函数,有两个参数。
(1)由服务器返回,并根据dataType参数进行处理后的数据。
(2)描述状态的字符串。
function(data, textStatus){undefined
//data可能是xmlDoc、jsonObj、html、text等等
this; //调用本次ajax请求时传递的options参数
}
11.error:
要求为Function类型的参数,请求失败时被调用的函数。该函数有3个参数,即XMLHttpRequest对象、错误信息、捕获的错误对象(可选)。ajax事件函数如下:
function(XMLHttpRequest, textStatus, errorThrown){undefined
//通常情况下textStatus和errorThrown只有其中一个包含信息
this; //调用本次ajax请求时传递的options参数
}
12.contentType:
要求为String类型的参数,当发送信息至服务器时,内容编码类型默认为"application/x-www-form-urlencoded"。该默认值适合大多数应用场合。
13.dataFilter:
要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。
function(data, type){undefined
//返回处理后的数据
return data;
}
14.dataFilter:
要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。
function(data, type){undefined
//返回处理后的数据
return data;
}
15.global:
要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。
16.ifModified:
要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。
17.jsonp:
要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。
18.username:
要求为String类型的参数,用于响应HTTP访问认证请求的用户名。
19.password:
要求为String类型的参数,用于响应HTTP访问认证请求的密码。
20.processData:
要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。
21.scriptCharset:
要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。
6.2 弹窗二次确认
先要下载layer,地址:https://github.com/sentsin/layer/releases/download/v3.5.1/layer-v3.5.1.zip
下载后放在项目的static目录,并在settings.py里配置好静态文件目录。
HTML代码:
Title
// 导入layer.js
ID
username
passowrd
edit
{% for foo in userObj %}
{{ foo.id }}
{{ foo.username }}
{{ foo.password }}
删除 /*这里a标签里没有href这是为了去掉它自身带的提交功能,还有个方法是: href="javascript:;"*/
{% endfor %}