Search code examples
mysqlpython-2.7utf-8decodedjango-1.9

DjangoUnicodeDecodeError on retrieving of bugs from bugzilla database ( MySQL)


Objective: Retrieving bugs from the Bugzilla database for analytic purpose, with Django framework, using Django:1.9.4, Python:2.7

Function to call function ( 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}) 

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)

    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

Displaying the bugs with few details ( 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 %}

It works fine as required except one case:

Bug summary

1.8 – Load Calendar: Operational Error displayed when user is trying to load calendar via URL.

After 1.8 it suppose to be hyphen(-), but its not, \x96 on retrieval.

Throwing an error:

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.' (<type 'str'>)

Requesting to help me on how to decode this character, tried a lot of links, no luck, help would be very much appreciated.


Solution

  • I have made a little changes on bugzilla.py which worked, added charset parameter on 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)
    

    Thank you.