中间件的django中间件错误不带任何参数

时间:2019-04-21 01:20:49

标签: django forum

自此以来,我一直遇到此错误,我已经检查了Django文档并通过google搜索了许多站点,但仍然没有得到针对此错误的解决方案,谢谢。每当我要运行服务器

时,都会出现此错误
from datetime import timedelta

from django.core.cache import cache
from django.utils import translation, timezone
from django.conf import settings as global_settings
import pytz
from djangobb_forum import settings as forum_settings

class TimezoneMiddleware(object):
    def process_request(self, request):
        if request.user.is_authenticated():
            profile = request.user.forum_profile
            try:
                timezone.activate(profile.time_zone)
            except pytz.UnknownTimeZoneError:
                profile.time_zone = global_settings.TIME_ZONE
                profile.save()

这是我在settings.py中使用python 3.7和Django 2.2的中间件

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

        #DjangoBB_Forum part
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'pagination.middleware.PaginationMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'djangobb_forum.middleware.LastLoginMiddleware',
    'djangobb_forum.middleware.UsersOnline',
    'djangobb_forum.middleware.TimezoneMiddleware',
]

这是错误消息

System check identified 48 issues (0 silenced).
April 21, 2019 - 00:34:43
Django version 2.2, using settings 'hayhealth_v_2.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Exception in thread Thread-1:
Traceback (most recent call last):
    File "c:\users\hay square\appdata\local\programs\python\python37-32\Lib\threading.py", line 917, in _bootstrap_inner
    self.run()
    File "c:\users\hay square\appdata\local\programs\python\python37-32\Lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
    handler = self.get_handler(*args, **options)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "C:\Users\hay square\Desktop\New folder (6)\all projects\hayhealth\hayhealth\hayhealth_v_2\wsgi.py", line 16, in <module>
    application = get_wsgi_application()
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\handlers\wsgi.py", line 135, in __init__
    self.load_middleware()
    File "C:\Users\HAYSQU~1\Desktop\NE86E9~1\ALLPRO~1\HAYHEA~1\HAYHEA~1\lib\site-packages\django\core\handlers\base.py", line 37, in load_middleware
    mw_instance = middleware(handler)
TypeError: TimezoneMiddleware() takes no arguments

2 个答案:

答案 0 :(得分:0)

为了使中间件正常工作,您似乎还需要使用一些其他代码。

1)根据这些instructions覆盖__init____call__

-或-

2)使用MiddlewareMixin

from django.utils.deprecation import MiddlewareMixin

class TimezoneMiddleware(MiddlewareMixin):
    ...

答案 1 :(得分:0)

在您的TimezoneMiddleware

中添加这两种方法
class TimezoneMiddleware(object):
    def __init__(self, get_response):
            self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)
    def process_request(self, request):
        ...