如果我使用fetchall()命令,Django Python会向我显示奇怪的结果

时间:2015-05-11 12:51:38

标签: python django

我使用了Django框架和MySQL数据库。我累了连续显示全名,它在html页面上显示了编码结果,如下所示。我试图在python上放置解码选项,但它不能正常工作。如果我使用fetchone()命令,它会显示一个正确的单词。但是,如果我使用fetchall()命令,它会显示不同的结果。你能看到错误吗?

profile.py

cursor = connection.cursor()
cursor.execute("SELECT full_name FROM myapp_profile ORDER BY idx DESC")
results = cursor.fetchall()

x = cursor.description
resultsList = []   
for r in results:
    i = 0
    d = {}
    while i < len(x):
        d[x[i][0]] = r[i]
        i = i+1
    resultsList.append(d)

context = Context({'data' : resultsList })
return HttpResponse(loader.get_template('profile/profile_list.html').render(context))

导致html

{'full_name': u'\uae40\uc9c0\uc120'} 
{'full_name': u'\uc774\uc8fc\ud604'} 
{'full_name': u'\uae40\uae30\uc790'} 
{'full_name': u'\uae40\uae30\uc131'} 
{'full_name': u'\uae40\uc544\uc601'} 

1 个答案:

答案 0 :(得分:0)

html代码错了..

之前就是这样......

{% if data %}
   There are {{ data|length }} records:
   {% for e in data %}
      <br />
      <td> {{ e }} </td>
      <br />
   {% endfor %}
{% else %}
   There are no records in the system
{% endif %}

我改变了这个,它起作用了

{% if data %}
   There are {{ data|length }} records:
   {% for e in data %}
      <br />
      <td> {{ e.full_name }} </td>
      <br />
   {% endfor %}
{% else %}
   There are no records in the system
{% endif %}