高职思政主题网站建设作用,象山县城乡和住房建设局网站,改网站字体颜色代码,seo网站首页优化排名怎么做前后端数据传输的编码格式#xff08;contentType#xff09;
我们只研究post请求方式的编码格式 #xff08; get请求方式没有编码格式和请求体#xff09; index?useranmepassword 参数直接在url地址的后面拼接着
有哪些方式可以提交post请求#xff1f;
for…前后端数据传输的编码格式contentType
我们只研究post请求方式的编码格式 get请求方式没有编码格式和请求体 index?useranmepassword 参数直接在url地址的后面拼接着
有哪些方式可以提交post请求
form表单 Ajax api工具
研究form表单的post请求
默认的编码格式urlencoded 数据传输的形式titledasdasprice2312datepublish2authors3
# 对于Django后端是如何接收数据的
把提交过来的数据都封装到了request.POST中
还可以提交form-data格式的
enctype:form-data # 提交文件数据 数据传输的形式 titledasdasprice2312datepublish2authors3 --------------binary----------------------------- 文件数据
#对于Django后端如何接收数据的
普通数据还是在request.POST中 文件数据呢还是在request.FILES中 之所以你能够在POST和FILES中接收数据是因为Django给你封装了提交过来的数据并不是queryDICT ajax提交post请求
默认情况下Ajax提交的数据后端还是在request.POST中接收的 默认的编码格式urlencoded 需要修改contentType类型json格式的
对于符合urlencoded格式的数据后端都是在request.POST中接收数据的
Ajax提交json格式的数据 {username:kevin, password:123} 它不符合urlencoded格式的数据# 在Django后端肯定在request.POST中接收不到数据
$.ajax({url:,type:post,data:JSON.stringify({a:1, b:2}), // 序列化的 {a:1, b:2}contentType:application/json, // json格式的success:function (res) {}})def index(request):if request.method POST:# request.POST只能接收post请求的符合urlencoded编码格式的数据 {}print(request.POST) # QueryDict: {}print(request.body) # b{a:1,b:2}json_bytesrequest.body # 接收浏览器发过来的纯原生的数据二进制需要自己做封装处理# json_strjson_bytes.decode(utf-8) # {a:1,b:2} class str# print(json_str, type(json_str))import json# json_dict json.loads(json_str)# print(json_dict, type(json_dict)) # {a: 1, b: 2} class dictjson_dictjson.loads(json_bytes) # 最好不要省略print(json_dict, type(json_dict)) # {a: 1, b: 2} class dictreturn render(request, index.html)
Ajax提交文件数据 script$(.btn).click(function (ev) {console.log(123);// 要获取到文件数据{#console.log($(#myfile)[0].files[0]) // C:\fakepath\123.png#}// 提交文件数据需要借助于formdata对象var myFormDataObj new FormData;var username $(#username).val();var myfile $(#myfile)[0].files[0];myFormDataObj.append(username, username);myFormDataObj.append(myfile,myfile);$.ajax({url: ,type: post,{#data: JSON.stringify({a: 1, b: 2}), // 序列化的 {a:1, b:2}#}data: myFormDataObj, // 序列化的 {a:1, b:2}{#contentType: application/json, // json格式的#}contentType:false, // 告诉浏览器不要给我的编码格式做任何的处理processData: false, //success: function (res) {}})})
/script
Ajax结合layer弹窗实现二次确认
layer 弹出层组件 - jQuery 弹出层插件
批量插入数据 bulk_list []for i in range(10000):user_objmodels.UserInfo(usernamekevin%s %i)bulk_list.append(user_obj)# 循环之后得到了一个列表10000个对象# 数据库的优化 同样的功能不同的sql执行的效率差距很大# 优化查询速度的时候首先想到的是加索引、优化sql语句有的sql走做引、有的sql不走索引models.UserInfo.objects.bulk_create(bulk_list)
分页的原理及推导
divmod
分页类
class Pagination(object):def __init__(self, current_page, all_count, per_page_num2, pager_count11):封装分页相关数据:param current_page: 当前页:param all_count: 数据库中的数据总条数:param per_page_num: 每页显示的数据条数:param pager_count: 最多显示的页码个数try:current_page int(current_page)except Exception as e:current_page 1if current_page 1:current_page 1self.current_page current_pageself.all_count all_countself.per_page_num per_page_num# 总页码all_pager, tmp divmod(all_count, per_page_num)if tmp:all_pager 1self.all_pager all_pagerself.pager_count pager_countself.pager_count_half int((pager_count - 1) / 2)propertydef start(self):return (self.current_page - 1) * self.per_page_numpropertydef end(self):return self.current_page * self.per_page_numpropertydef page_html(self):# 如果总页码 11个if self.all_pager self.pager_count:pager_start 1pager_end self.all_pager 1# 总页码 11else:# 当前页如果页面上最多显示11/2个页码if self.current_page self.pager_count_half:pager_start 1pager_end self.pager_count 1# 当前页大于5else:# 页码翻到最后if (self.current_page self.pager_count_half) self.all_pager:pager_end self.all_pager 1pager_start self.all_pager - self.pager_count 1else:pager_start self.current_page - self.pager_count_halfpager_end self.current_page self.pager_count_half 1page_html_list []# 添加前面的nav和ul标签page_html_list.append(nav aria-labelPage navigationul classpagination)first_page lia href?page%s首页/a/li % (1)page_html_list.append(first_page)if self.current_page 1:prev_page li classdisableda href#上一页/a/lielse:prev_page lia href?page%s上一页/a/li % (self.current_page - 1,)page_html_list.append(prev_page)for i in range(pager_start, pager_end):if i self.current_page:temp li classactivea href?page%s%s/a/li % (i, i,)else:temp lia href?page%s%s/a/li % (i, i,)page_html_list.append(temp)if self.current_page self.all_pager:next_page li classdisableda href#下一页/a/lielse:next_page lia href?page%s下一页/a/li % (self.current_page 1,)page_html_list.append(next_page)last_page lia href?page%s尾页/a/li % (self.all_pager,)page_html_list.append(last_page)# 尾部添加标签page_html_list.append(/nav/ul)return .join(page_html_list)