1.问题截图
image.png2.关键代码
def news_tech(request):
db = pymysql.connect(host="localhost", user="root", passwd="12345678", db="toutiao")
with db.cursor() as cursor:
item = {}
cursor.execute("select title, ...省略...from news_tech")
ret = cursor.fetchone()
item["title"] = ret[0]
return HttpResponse(json.dumps(item, ensure_ascii=False))
3.解决
在HttpResponse中设置ensure_ascii=False还是不够的,还需要在连接数据库时设置编码:charset="utf8"
db = pymysql.connect(host="localhost", user="root",
passwd="12345678", db="toutiao", charset="utf8")
编码成功
网友评论