2

At the beginning I wanted to say hello and say that I have been using the portal for several years, but now I decided to create an account and describe my problem.

I try to display the table, unfortunately there is still an error when I open view.html:

UnicodeDecodeError at /add 'utf-8' codec can't decode byte 0xb3 in position 8: invalid start byte

For a few hours I was looking for a solution and unfortunately I could not solve this problem. I use Polish characters, in the database characters are saved as utf-8, if I remove the Polish characters from the database it all works, but that's not what I want.

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 %}

error

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

In database (5.5.62-cll - MySQL Community Server) utf8 is set per table and text field. Coding of server characters: UTF-8 Unicode.

Gregory
  • 31
  • 3
  • Have you tried [these guidelines for setting up UTF-8 on Django/Mysql](https://stackoverflow.com/questions/10061842/how-to-set-django-and-mysql-work-with-utf-8)? Also, how exactly are you putting the Polish characters into the database? – krubo Nov 05 '19 at 21:29
  • Thank you for your suggestions. Yes, I have already tried these steps. I entered the characters into the database via phpmyadmin, it didn't work I removed the table and prepared an import file .sql made in notepad ++ (in the format tab I chose utf-8 encoding). I also clicked convert to utf8 to be sure. – Gregory Nov 05 '19 at 21:43
  • I bet the .sql file is somehow the problem, even though you chose utf-8 in notepad++ it might still get broken in the import process. Can you try using the [Django admin site](https://docs.djangoproject.com/en/2.2/ref/contrib/admin/) to enter the data instead? – krubo Nov 05 '19 at 21:57
  • I added some records by django admin and Polish characters are displayed correctly on views.html, but in the database via phpmyadmin, adminer.php, or when I export to .sql and open notepad++, it's everywhere are displayed in a strange way "Ĺ"=>"ł" "Ä"=>"ć" "Ĺş"=>"ź" Exception location is: /usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py in _fetch_row, line 325 I'm using MySQLdb, maybe I still need to set the encoding somewhere else. – Gregory Nov 05 '19 at 23:48
  • The mappings you mentioned can be understood as follows: encoding "ź" in [UTF-8 gives](https://onlineutf8tools.com/convert-utf8-to-hexadecimal) `0xc5 0xba`, and decoding this in [ISO 8859-2](https://en.wikipedia.org/wiki/ISO/IEC_8859-2) gives "Ĺş". However, it's unclear to me why the UTF-8 is getting decoded as ISO 8859-2. Do any of [these MySQL encoding solutions](https://stackoverflow.com/questions/4777900/how-to-display-utf-8-characters-in-phpmyadmin) help? – krubo Nov 06 '19 at 10:54
  • Thank you for your help, your tips helped me to verify this problem. I updated the version of python, django and tried on another Linux distribution, but it was still a bug, so I changed the database engine to MariaDB and now it works. – Gregory Nov 11 '19 at 12:01

0 Answers0