在django 1.10中,如何将变量传递给中间件异常处理程序?
在下面的例子中,我想访问process_exception()中传递的列表([1,2,3])。
views.py
class my_exception(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def my_view(request):
...
my_var = [1,2,3] # Want to pass this to exception middleware
raise my_exception(my_var)
middleware.py
from django.utils.deprecation import MiddlewareMixin
class ExceptionMiddleware(MiddlewareMixin):
# Has been added to settings.py middleware list
def process_exception(self, request, exception):
if type(exception) == my_exception:
# How to access my_var here?
...
答案 0 :(得分:2)
它只是exception.value
,因为异常与您正在检查的类匹配。该例外将包含您用于创建它的数据。