Django项目启动 AttributeError: ‘str‘ object has no attribute ‘decode‘ 问题
作者:互联网
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "D:\Anaconda3\lib\threading.py", line 980, in _bootstrap_inner self.run() File "D:\Anaconda3\lib\threading.py", line 917, in run self._target(*self._args, **self._kwargs) File "D:\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "D:\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run self.check_migrations() File "D:\Anaconda3\lib\site-packages\django\core\management\base.py", line 453, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "D:\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "D:\Anaconda3\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__ self.build_graph() File "D:\Anaconda3\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "D:\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations if self.has_table(): File "D:\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "D:\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor return self._cursor() File "D:\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor self.ensure_connection() File "D:\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection self.connect() File "D:\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 197, in connect self.init_connection_state() File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 231, in init_connection_state if self.features.is_sql_auto_is_null_enabled: File "D:\Anaconda3\lib\site-packages\django\utils\functional.py", line 80, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\features.py", line 82, in is_sql_auto_is_null_enabled cursor.execute('SELECT @@SQL_AUTO_IS_NULL') File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 103, in execute sql = self.db.ops.last_executed_query(self.cursor, sql, params) File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query query = query.decode(errors='replace') AttributeError: 'str' object has no attribute 'decode' D:\Anaconda3\Lib\site-packages\django\db\backends\mysql\operations.py changed, reloading. Performing system checks... Watching for file changes with StatReloader System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 82, in _execute return self.cursor.execute(sql) File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "D:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 148, in execute result = self._query(query) File "D:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 310, in _query conn.query(q) File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 548, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result result.read() File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 1156, in read first_packet = self.connection._read_packet() File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 725, in _read_packet packet.raise_for_error() File "D:\Anaconda3\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) File "D:\Anaconda3\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.OperationalError: (1046, 'No database selected') The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\Anaconda3\lib\threading.py", line 980, in _bootstrap_inner self.run() File "D:\Anaconda3\lib\threading.py", line 917, in run self._target(*self._args, **self._kwargs) File "D:\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "D:\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run self.check_migrations() File "D:\Anaconda3\lib\site-packages\django\core\management\base.py", line 453, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "D:\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "D:\Anaconda3\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__ self.build_graph() File "D:\Anaconda3\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "D:\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations if self.has_table(): File "D:\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "D:\Anaconda3\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names return get_names(cursor) File "D:\Anaconda3\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names return sorted(ti.name for ti in self.get_table_list(cursor) File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\introspection.py", line 55, in get_table_list cursor.execute("SHOW FULL TABLES") File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 99, in execute return super().execute(sql, params) File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "D:\Anaconda3\lib\site-packages\django\db\utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "D:\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 82, in _execute return self.cursor.execute(sql) File "D:\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "D:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 148, in execute result = self._query(query) File "D:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 310, in _query conn.query(q) File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 548, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result result.read() File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 1156, in read first_packet = self.connection._read_packet() File "D:\Anaconda3\lib\site-packages\pymysql\connections.py", line 725, in _read_packet packet.raise_for_error() File "D:\Anaconda3\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) File "D:\Anaconda3\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) django.db.utils.OperationalError: (1046, 'No database selected')
Django启动时报错
问题原因:
-
从 Error信息中可以得知,这是 str编码的问题,而且还是和 mysql进行交互之间的编码问题。
-
python3里面,字符串要先encode手动指定其为某一编码的字节码之后,才能decode解码。
SO:
点击最后一条异常信息的地址,将query = query.decode(errors='replace') 修改成 query = query.encode(errors='replace')
标签:py,lib,Anaconda3,attribute,object,Django,File,line,packages 来源: https://www.cnblogs.com/hkwJsxl/p/16513232.html