关于从bugzilla数据库(MySQL)中检索bug的DjangoUnicodeDecodeError

时间:2016-04-11 03:38:54

标签: mysql python-2.7 utf-8 decode django-1.9

目标:使用Django:1.9.4Python:2.7

从Djzo框架中检索Bugzilla数据库中的错误以进行分析

调用函数的函数(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 - 加载日历:当用户尝试通过URL加载日历时显示操作错误。

1.8之后,它假定为连字符( - ),但在检索时不是\x96

抛出错误:

/ customerBugs / 中的DjangoUnicodeDecodeError

'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'>)

请求帮助我如何解码这个角色,尝试了很多链接,没有运气,帮助将非常感激。

2 个答案:

答案 0 :(得分:1)

hex 96是“EM DASH”()的latin1编码;十六进制E28093是utf8编码。

您需要在编码和声明中保持一致,最好是utf8。

检查表/列是否也是CHARACTER SET utf8

如果您想使用latin1,请将Django character_encoding更改为“latin1”。

答案 1 :(得分:0)

我对 bugzilla.py 做了一些改动,它在MySQLdb.connect()上添加了charset参数

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)

谢谢。