从 bugzilla 数据库中检索错误时出现 DjangoUnicodeDecodeError (MySQL)
DjangoUnicodeDecodeError on retrieving of bugs from bugzilla database ( MySQL)
Objective: 从 Bugzilla 数据库中检索错误以进行分析,使用 Django 框架,使用 Django:1.9.4
、Python:2.7
函数调用函数(view.py)
@login_required(login_url='login/')
def customerBugs(request):
tag_id = request.GET['tag_id']
request.session['tagid'] = tag_id
tag_id = int(tag_id)
customer_names = {1:'Customer1', 2:'Customer2', 3:'Customer3'}
tag_name = customer_names[tag_id]
request.session[0] = tag_name
listbugs1 = list(listBugs(tag_name, request))
return render(request, 'customerbugs.html', {'listbugs1': listbugs1, 'tag_name': tag_name})
获取详情的函数(bugzilla.py)
def listBugs(tag_id,request):
db = MySQLdb.connect(host=DBhost, port=DBport, user=DBuser, passwd=DBpassword , db=DBdatabase)
sql1 = ('SELECT bug_id, short_desc, bug_status FROM bugzilla.bugs WHERE cf_customer_name = "%s"' %tag_id)
cursor = db.cursor()
cursor.execute(sql1)
data2 = cursor.fetchall()
db.close()
print data2
return data2
显示错误的细节很少(displayBugs.html)
{% extends 'base.html' %}
{% block content %}
<form method="" action="/downloadReport/">
<button type="submit" name="tag_id" value="{{request.session.tagid }}">Download</button>
</form>
<form method="get" action="/customerBugStatus/">
<button type="submit" style="float: right;" name="status_id" value="Open">Open Bugs
</button>
</form>
<table border="1" style="width:100%">
<h2> Customer name: {{ tag_name }}</h2>
<tr>
<th>Bug ID</th>
<th>Dscription</th>
<th>Status</th>
</tr>
{% for bug in listbugs1 %}
<tr>
<td>
{{ bug.0 }}</a>
</td>
<td>
{{ bug.1 }}</a>
</td>
<td>
{{ bug.2 }}</a>
</td>
</tr>
{% endfor %}
{% endblock %}
除了一种情况外,它按要求工作正常:
错误总结
1.8 – Load Calendar: Operational Error displayed when user is trying to load calendar via URL.
在 1.8
之后它应该是连字符 (-),但它不是,\x96
在检索时。
抛出错误:
DjangoUnicodeDecodeError 在 /customerBugs/
'utf8' codec can't decode byte 0x96 in position 4: invalid start byte. You passed in '1.8 \x96 Load Calendar: Operational Error displayed when user is trying to load calendar via URL.' (<type 'str'>)
请求帮助我如何解码这个字符,尝试了很多链接,没有运气,帮助将不胜感激。
hex 96
是 "EM DASH" (–
) 的 latin1 编码; hex E28093
是utf8编码。
编码和声明需要保持一致,最好是utf8。
检查 table/column 是否也是 CHARACTER SET utf8
。
如果您想使用 latin1,请将 Django character_encoding
更改为 "latin1"。
我对 bugzilla.py 做了一些小改动,效果很好,在 MySQLdb.connect()
上添加了字符集参数
Function to get the details ( bugzilla.py)
def listBugs(tag_id,request):
db = MySQLdb.connect(host=DBhost, port=DBport, user=DBuser, passwd=DBpassword , db=DBdatabase, charset='utf8')
sql1 = ('SELECT bug_id, short_desc, bug_status FROM bugzilla.bugs WHERE cf_customer_name = "%s"' %tag_id)
谢谢。
Objective: 从 Bugzilla 数据库中检索错误以进行分析,使用 Django 框架,使用 Django:1.9.4
、Python:2.7
函数调用函数(view.py)
@login_required(login_url='login/')
def customerBugs(request):
tag_id = request.GET['tag_id']
request.session['tagid'] = tag_id
tag_id = int(tag_id)
customer_names = {1:'Customer1', 2:'Customer2', 3:'Customer3'}
tag_name = customer_names[tag_id]
request.session[0] = tag_name
listbugs1 = list(listBugs(tag_name, request))
return render(request, 'customerbugs.html', {'listbugs1': listbugs1, 'tag_name': tag_name})
获取详情的函数(bugzilla.py)
def listBugs(tag_id,request):
db = MySQLdb.connect(host=DBhost, port=DBport, user=DBuser, passwd=DBpassword , db=DBdatabase)
sql1 = ('SELECT bug_id, short_desc, bug_status FROM bugzilla.bugs WHERE cf_customer_name = "%s"' %tag_id)
cursor = db.cursor()
cursor.execute(sql1)
data2 = cursor.fetchall()
db.close()
print data2
return data2
显示错误的细节很少(displayBugs.html)
{% extends 'base.html' %}
{% block content %}
<form method="" action="/downloadReport/">
<button type="submit" name="tag_id" value="{{request.session.tagid }}">Download</button>
</form>
<form method="get" action="/customerBugStatus/">
<button type="submit" style="float: right;" name="status_id" value="Open">Open Bugs
</button>
</form>
<table border="1" style="width:100%">
<h2> Customer name: {{ tag_name }}</h2>
<tr>
<th>Bug ID</th>
<th>Dscription</th>
<th>Status</th>
</tr>
{% for bug in listbugs1 %}
<tr>
<td>
{{ bug.0 }}</a>
</td>
<td>
{{ bug.1 }}</a>
</td>
<td>
{{ bug.2 }}</a>
</td>
</tr>
{% endfor %}
{% endblock %}
除了一种情况外,它按要求工作正常:
错误总结
1.8 – Load Calendar: Operational Error displayed when user is trying to load calendar via URL.
在 1.8
之后它应该是连字符 (-),但它不是,\x96
在检索时。
抛出错误:
DjangoUnicodeDecodeError 在 /customerBugs/
'utf8' codec can't decode byte 0x96 in position 4: invalid start byte. You passed in '1.8 \x96 Load Calendar: Operational Error displayed when user is trying to load calendar via URL.' (<type 'str'>)
请求帮助我如何解码这个字符,尝试了很多链接,没有运气,帮助将不胜感激。
hex 96
是 "EM DASH" (–
) 的 latin1 编码; hex E28093
是utf8编码。
编码和声明需要保持一致,最好是utf8。
检查 table/column 是否也是 CHARACTER SET utf8
。
如果您想使用 latin1,请将 Django character_encoding
更改为 "latin1"。
我对 bugzilla.py 做了一些小改动,效果很好,在 MySQLdb.connect()
Function to get the details ( bugzilla.py)
def listBugs(tag_id,request):
db = MySQLdb.connect(host=DBhost, port=DBport, user=DBuser, passwd=DBpassword , db=DBdatabase, charset='utf8')
sql1 = ('SELECT bug_id, short_desc, bug_status FROM bugzilla.bugs WHERE cf_customer_name = "%s"' %tag_id)
谢谢。