【问题标题】:DjangoUnicodeDecodeError on retrieving of bugs from bugzilla database ( MySQL)DjangoUnicodeDecodeError 从 bugzilla 数据库 ( MySQL ) 中检索错误
【发布时间】:2016-08-01 01:39:17
【问题描述】:

目标:使用Django:1.9.4,Python:2.7,使用Django 框架从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 在检索时。

抛出错误:

DjangoUnicodeDecodeError at /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.' (&lt;type 'str'&gt;)

请求帮助我如何解码这个字符,尝试了很多链接,但没有运气,非常感谢帮助。

【问题讨论】:

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


    【解决方案1】:

    我对 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)
    

    谢谢。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 表默认编码为 utf8 ENGINE=InnoDB AUTO_INCREMENT=5649 DEFAULT CHARSET=utf8;,我不想使用 latin1。
      • latin1 96 来自某个地方。找到源头并修复它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2021-10-07
      相关资源
      最近更新 更多