Django | Cookie 中文编码的问题
在Django中,向cookie写入中文字符后会报错;如向cookie中保存用户名,当用户名存在中文字符时:

Traceback (most recent call last):
File , in run
self.finish_response()
File , in finish_response
self.write(data)
File , in write
self.send_headers()
File , in send_headers
self._write(bytes(self.headers))
File , in __bytes__
return str(self).encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 145-146: ordinal not in range(256)
[/Apr/ ::]
----------------------------------------
Exception happened during processing of request )
Traceback (most recent call last):
File , in run
self.finish_response()
File , in finish_response
self.write(data)
File , in write
self.send_headers()
File , in send_headers
self._write(bytes(self.headers))
File , in __bytes__
return str(self).encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 145-146: ordinal not in range(256)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File , in run
self.handle_error()
File , in handle_error
super(ServerHandler, self).handle_error()
File , in handle_error
self.finish_response()
File , in finish_response
self.write(data)
File , in write
self.send_headers()
File , in send_headers
if not self.origin_server or self.client_is_modern():
File , in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File , in process_request_thread
self.finish_request(request, client_address)
File , in finish_request
self.RequestHandlerClass(request, client_address, self)
File , in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File , in __init__
self.handle()
File , in handle
handler.run(self.server.get_app())
File , in run
self.close()
File , in close
self.status.split()[], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
此时可以使用Json模块的dumps()和loads(),将其序列化,再进行反序列化;
如记录用户名时,先将用户名进行序列化,再写入到cookie中。而在读取cookie之后,再将其反序列化即可
dumps / loads 用法:
import json username='用户1' username=json.dumps(username) username '"\\u7528\\u62371"' # 反序列化 username=json.loads(username) username '用户1'
在Django中:
if remember=='on':
# 记住用户名
# 如果username是中文,设置cookies时会报错
# cookie 中文编码处理
username=json.dumps(username)
response.set_cookie('username',username,max_age=7*24*3600)
else:
# 取消记住用户名
response.delete_cookie('username')
if 'username' in request.COOKIES:
username=request.COOKIES.get('username')
username=json.loads(username)
Django | Cookie 中文编码的问题的更多相关文章
- Django cookie相关操作
Django cookie 的相关操作还是比较简单的 首先是存储cookie #定义设置cookie(储存) def save_cookie(request): #定义回应 response = Ht ...
- falsk 与 django cookie和session存、取、删的区别
falsk cookie的存取删需导入from flask import Flask,make_response,request# 存COOKIE的方法@app.route('/setcookie') ...
- django cookie、session
Cookie.Session简介: Cookie.Session是一种会话跟踪技术,因为http请求都是无协议的,无法记录上一次请求的状态,所以需要cookie来完成会话跟踪,Seesion的底层是由 ...
- Python之路-(Django(Cookie、分页))
Cookie 分页 1.获取Cookie: request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, sal ...
- Django Cookie 和 Sessions 应用
在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...
- Python Web框架篇:Django cookie和session
part 1 概念 在Django里面,cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话. 两者最大的区别是cookie的信息是存放在浏览器客户端的,而sessio ...
- 28.Django cookie
概述 1.获取cookie request.COOKIES['key'] request.COOKIES.get('key') request.get_signed_cookie(key, defau ...
- 5.Django cookie
概述 1.获取cookie request.COOKIES['key'] request.COOKIES.get('key') request.get_signed_cookie(key, defau ...
- Django Cookie,Session
Cookie Cookie的由来 HTTP协议是无状态的,每次请求都是独立的,对服务器来说,每次的请求都是全新的,上一次的访问是数 据是无法保留到下一次的 某些场景需要状态数据或者中间数据等相关对下一 ...
随机推荐
- MySQL优化(二) 优化诀窍
一.索引的使用 (1)查询要使用索引最重要的条件是查询条件中的字段建立了索引: (2)下列几种情况可能使用到索引: <1> 对于创建的多列索引,只要查询条件使用了最坐边的列,索引一般就会被 ...
- 谷歌开源的一个BTREE实现 Go语言
// Copyright 2014 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "Licens ...
- Android单片机与蓝牙模块通信实例代码
Android单片机与蓝牙模块通信实例代码 参考路径:http://www.jb51.net/article/83349.htm 啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通 ...
- JS基础-运算符-函数
1.运算符 1.赋值运算符和扩展运算符 1.赋值运算符 = 2.扩展运算符 +=,-=,*=,/=,%=,^=.... ex: a=a+b;--> ...
- JNI 记
Java Native Interface(JNI)从零开始详细教程 ================================================================ ...
- sas通过IMPORT过程读取外部文件数据
SAS通过IMPORT过程读取外部文件数据 使用IMPORT过程导入带分隔符的文件外,Microsoft Access数据库文件.Miscrosft Excel工作簿. dBase文件.JMP文件.S ...
- 学以致用十九-----shell脚本之引号
shell脚本中引号也是有讲究的. 可以分为单引号‘ ’,双引号“ ”和 反引号 ` ` 具体区别来看一个例子 输出结果 可得出结论 1. 单引号括起来的字符都作为普通字符出现,如第4,7,10,13 ...
- 网络编程—端口分类调研和netstat命令
运输层的端口: 1.什么是端口?为甚要使用用端口? 进程的创建和撤销都是动态的,通信的一方几乎无法识别对方机器上的进程,我们需要利用目的主机提供的功能来识别终点. 所以为了解决上述问题,我们就在运输层 ...
- 检索COM类工厂中CLSID为{10020100-E260-11CF-AE68-00AA004A34D5}的组件时失败,原因是出现以下错误:80040154
{"检索 COM 类工厂中 CLSID 为 {10020100-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80040154."} ...
- Log system architecture
0. 技术选型参考 1. Collector Keywords: Collector, Processor 名称 Beats Fluentd-bit Introduction Beats are a ...