gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:11:19 25 4
gpt4 key购买 nike

我启动了 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连接的错误日志。我在 AWS RDS 中外部配置了测试 mysql 数据库。所以这将是线索。

........
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)
........

最佳答案

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

请参阅 Trouble with UTF-8 characters; what I see is not what I stored 中的“最佳实践”

关于python - 无法从 MySQL 获取数据并在 Django 中呈现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60034897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com