python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'
在centos7中使用uwsgi启动django项目 项目连接mysql
在进行数据获取的时候报错如下
报错信息
Internal Server Error: /api/v1/gamefile/1 Traceback (most recent call last): File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/rest_framework/views.py", line 497, in dispatch response = self.handle_exception(exc) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/rest_framework/views.py", line 457, in handle_exception self.raise_uncaught_exception(exc) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/rest_framework/views.py", line 468, in raise_uncaught_exception raise exc File "/root/.virtualenvs/v1/lib/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch response = handler(request, *args, **kwargs) File "./app/views.py", line 77, in get if res: File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/models/query.py", line 278, in __bool__ self._fetch_all() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1098, in execute_sql cursor = self.connection.cursor() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 256, in cursor return self._cursor() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 233, in _cursor self.ensure_connection() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection self.connect() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 197, in connect self.init_connection_state() File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 231, in init_connection_state if self.features.is_sql_auto_is_null_enabled: File "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/root/.virtualenvs/v1/lib/python3.6/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 "/root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/utils.py", line 103, in execute sql = self.db.ops.last_executed_query(self.cursor, sql, params) File "/root/.virtualenvs/v1/lib/python3.6/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'
解决方法
找到报错行,将其注释掉
vim /root/.virtualenvs/v1/lib/python3.6/site-packages/django/db/backends/mysql/operations.py #找到146行,将其注释掉