无法从MySQL获取数据并在Django中呈现

2024-05-03 19:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我启动了Django项目来维护服务。然后我得到了以下错误

UnicodeDecodeError at /register/
'utf-8' codec can't decode byte 0xb7 in position 3: invalid start byte 

使用模板呈现页面是非常简单的操作。Django说模板文件中发生了错误,但被解码为UTF-8

工作环境是,

- Python 3.6.10 installed by pyenv.
- Django 2.1.8
- macOS Catalina

更新

在错误日志中,我找到了关于mysql连接的erorr日志。我在AWS RDS中外部配置了测试mysql DB。所以这就是线索

........
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
  1186.             self._result_cache = list(self._iterable_class(self))
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1065.             cursor.execute(sql, params)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  100.             return super().execute(sql, params)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)
File "/Users/jinhoyoo/.pyenv/versions/3.6.10/envs/youha/lib/python3.6/site-packages/django/db/backends/mysql/base.py" in execute
  71.             return self.cursor.execute(query, args)
........

Tags: djangoinselfpyenvexecutedblibpackages
2条回答

我找到了原因。MySQL 8.0.19版本存在此问题。您应该使用8.0.18下的mysql版本作为客户端

您只能通过brew install mysql-client安装mysql客户端,然后您将获得mysql客户端8.0.18。然后按照https://github.com/PyMySQL/mysqlclient-python重新安装mysqlclientpython模块

B7是拉丁文1中“中间点”的十六进制。听起来您的客户机正在使用拉丁语1,但您的数据库设置需要utf8

Trouble with UTF-8 characters; what I see is not what I stored中的“最佳做法”

相关问题 更多 >