weijiang1994 / Blogin

A blog system depend on flask.使用flask框架开发的个人博客系统.
https://2dogz.cn
GNU General Public License v3.0
509 stars 99 forks source link

flask admin initialize db throw UnicodeDecodeError : codec can't decode byte 0xae in position 129: illegal multibyte sequence #1

Closed wakakaaaabbbb closed 3 years ago

weijiang1994 commented 4 years ago

please confirm your database encode is utf8.

weijiang1994 commented 4 years ago

因为在初始化数据库的时候在状态表中插入两个状态,使用中文命名的。代码如下 Blogin/blogin/__init__.py文件

@app.cli.command()
def admin():
        try:
            db.drop_all()
            db.create_all()
            Role.init_role()
            States.init_states()
# 省略后续代码

/Blogin/blogin/models.py文件

@staticmethod
def init_states():
        s1 = States(name='正常')
        s2 = States(name='禁用')
        db.session.add(s1)
        db.session.add(s2)
        db.session.commit()

所以报这个错误应该是你数据库的编码没有设置为utf8

wakakaaaabbbb commented 4 years ago

我高估我自己了哈哈哈,下面是error (venv) D:\project\createblog\Blogin>flask admin Traceback (most recent call last): File "C:\python385\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\python385\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "D:\project\createblog\Blogin\venv\Scripts\flask.exe__main.py", line 7, in File "d:\project\createblog\blogin\venv\lib\site-packages\flask\cli.py", line 967, in main cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None) File "d:\project\createblog\blogin\venv\lib\site-packages\flask\cli.py", line 575, in main load_dotenv() File "d:\project\createblog\blogin\venv\lib\site-packages\flask\cli.py", line 649, in load_dotenv dotenv.load_dotenv(path) File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\main.py", line 312, in load_dotenv return DotEnv(f, verbose=verbose, interpolate=interpolate, **kwargs).set_as_environment_variables(override=override) File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\main.py", line 102, in set_as_environment_variables for k, v in self.dict().items(): File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\main.py", line 86, in dict values = OrderedDict(self.parse()) File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\main.py", line 93, in parse for mapping in with_warn_for_invalid_lines(parse_stream(stream)): File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\main.py", line 48, in with_warn_for_invalid_lines for mapping in mappings: File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\parser.py", line 235, in parse_stream reader = Reader(stream) File "d:\project\createblog\blogin\venv\lib\site-packages\dotenv\parser.py", line 107, in init__ self.string = stream.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 129: illegal multibyte sequence

wakakaaaabbbb commented 4 years ago

mysql> show variables like '%char%' -> ; +--------------------------+----------------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | D:\mysql\mysql-8.0.20-winx64\share\charsets\ | +--------------------------+----------------------------------------------+ 8 rows in set, 1 warning (0.00 sec)

weijiang1994 commented 4 years ago

问题解决了吗? 你可以手动插入中文数据到你的数据库试试,看看会不会报错。

weijiang1994 commented 4 years ago

可能是你的 .env文件的编码不对 看你的报错还没到数据库那一步

wakakaaaabbbb commented 4 years ago

也不是env编码的问题,下班再看看hhh