我在运行的django项目(1.11)中有这种奇怪的行为(例如,来自开发服务器的示例):首先,我收到记录错误,这可能与设置有关 但是,真正奇怪的是:每当引发异常时,都会导致Unicode错误。我假设语言设置或python版本干扰可能存在问题?
这是视图-我添加了零除以导致错误。当我删除它时,一切正常:
class TestView(TemplateView):
template_name = "table.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
dummy=1/0
return context
以下是错误日志:
--- Logging error ---
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49868)
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
Unhandled exception in thread started by <bound method Thread._bootstrap of <Thread(Thread-1, started daemon -173327552)>>
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
File "/home/franzritt/sky/monitoring/views.py", line 19, in get_context_data
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib/python3.4/traceback.py", line 153, in _format_exception_iter
yield from _format_list_iter(_extract_tb_iter(tb, limit=limit))
File "/usr/lib/python3.4/traceback.py", line 18, in _format_list_iter
for filename, lineno, name, line in extracted_list:
File "/usr/lib/python3.4/traceback.py", line 65, in _extract_tb_or_stack_iter
line = linecache.getline(filename, lineno, f.f_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 15, in getline
lines = getlines(filename, module_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 41, in getlines
return updatecache(filename, module_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 127, in updatecache
lines = fp.readlines()
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 957: invalid continuation byte
我已经在这里显示-我认为-错误日志的重要部分。记录错误和unicode错误会显示更多次。所以我的基本问题是,每当发生异常时,我都会得到20个Unicode期望值和10个记录错误,这使得很难找到真正的异常。
答案 0 :(得分:1)
您的模型 str 无法正确处理Unicode。我建议您使用UTF-8编码。为此,请将其放在您的.py
模块/文件的开头:
# -*- coding: utf-8 -*-
如果这不起作用,请尝试:
def __str__(self):
return self.name.encode('utf8')
或使用:
class TestView(TemplateView):
template_name = "table.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
dummy=1/0
return context.encode('utf-8')