一开始我想打个招呼,说我已经使用门户好几年了,但是现在我决定创建一个帐户并描述我的问题。
我尝试显示该表,但不幸的是,当我打开view.html时仍然出现错误:
/ p中的UnicodeDecodeError 'utf-8'编解码器无法解码位置8的字节0xb3:无效的起始字节
几个小时以来,我一直在寻找解决方案,但不幸的是我无法解决此问题。 我使用波兰语字符,在数据库中将字符保存为utf-8,如果我从数据库中删除波兰语字符,则一切正常,但这不是我想要的。
models.py
from django.db import models
class komDane (models.Model):
data = models.TextField()
product = models.TextField()
oldprice = models.TextField()
newprice = models.TextField()
dostepnosc = models.IntegerField()
def __str__(self):
return self.name
views.py
from django.shortcuts import render,render_to_response
import mysql.connector
from .models import komDane
import sys
def index(request):
dupa = "nic"
return render_to_response('index.html',{'name':'dupa'})
def add(request):
zwraca = komDane.objects.all()
return render_to_response('result.html',{'dane': zwraca})
views.html
{% extends 'head1.html' %}
{% block content %}
<table><tbody>
{% for a in dane %}
<tr><td>{{ a.id }}</td><td>{{ a.product }}</td><td>{{ a.oldprice }}</td><td>{{ a.newprice }}</td><td>{{ a.newprice }}</td></tr>
{% endfor %}
</table></tbody>
{% endblock %}
错误
Request URL: http://192.168.1.12:8001/add
Django Version: 2.2.6
Exception Type: UnicodeDecodeError
Exception Value:
'utf-8' codec can't decode byte 0xb3 in position 8: invalid start byte
Exception Location: /usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py in _fetch_row,line 325
Python Executable: /usr/bin/python3
Python Version: 3.5.3
Python Path:
['/home/pi/Desktop/mysite','/usr/lib/python35.zip','/usr/lib/python3.5','/usr/lib/python3.5/plat-arm-linux-gnueabihf','/usr/lib/python3.5/lib-dynload','/usr/local/lib/python3.5/dist-packages','/usr/lib/python3/dist-packages']
Server time: Wt,5 Lis 2019 21:18:21 +0100
在数据库(5.5.62-cll-MySQL Community Server)中,为每个表和文本字段设置utf8。服务器字符编码:UTF-8 Unicode。