TypeError: CheckNewsDateStatus() takes no arguments
TypeError: CheckNewsDateStatus() takes no arguments
我正在尝试将 django/mezzanine 应用程序从 python 2.7 更新到 python 3.7。你能帮我修正下面的错误吗(CheckNewsDateStatus() takes no arguments
)?
好像这个class根本没用过;如果我 grep 通过所有代码,只有附加的 settings.py 和 middleware.py 匹配。它是在 django/mezzanine 中部分实现的,还是它使得整个 class 可以被删除?
我不知道代码是如何计划工作的,我也不知道该功能是否已被使用...
(python-3.7) miettinj@ramen:~/pika> python manage.py runserver
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
Watching for file changes with StatReloader
.....
_d^^^^^^^^^b_
.d'' ``b.
.p' `q.
.d' `b.
.d' `b. * Mezzanine 5.0.0
:: :: * Django 2.2
:: M E Z Z A N I N E :: * Python 3.7.10
:: :: * PostgreSQL 9.3.0
`p. .q' * Linux 5.3.18-lp152.102-default
`p. .q'
`b. .d'
`q.. ..p'
^q........p^
''''
Performing system checks...
System check identified no issues (0 silenced).
December 30, 2021 - 13:44:54
Django version 2.2, using settings 'pika.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/python/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/opt/python/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 170, in inner_run
super().inner_run(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 173, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 42, in get_internal_wsgi_application
return get_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: CheckNewsDateStatus() takes no arguments
middlewary.py:
class CheckNewsDateStatus(object):
def process_request(self, request):
if '/uutinen/' in request.path:
try:
path_to_go_raw = request.path
true_slug = path_to_go_raw.split('/uutinen/')[1:]
news_obj = Uutinen.objects.get(slug=true_slug[0])
now_utc = pytz.utc.localize(datetime.now())
hel = pytz.timezone("Europe/Helsinki")
foo = news_obj.publish_date.astimezone(hel)
if foo.date() < now_utc.date() and news_obj.status == 2:
pass
elif foo.date() == now_utc.date() and foo.time() < now_utc.time() and news_obj.status == 2:
pass
else:
if request.user.is_authenticated():
pass
elif news_obj.status == 1:
return HttpResponseNotFound('404')
else:
return HttpResponseNotFound('404')
except:
pass
settings.py:
# List of middleware classes to use. Order is important; in the request phase,
# these middleware classes will be applied in the order given, and in the
# response phase the middleware will be applied in reverse order.
MIDDLEWARE = (
'page_types.middleware.SetDynamicSite',
# 'debug_toolbar.middleware.DebugToolbarMiddleware',
"mezzanine.core.middleware.UpdateCacheMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
#"django.middleware.locale.LocaleMiddleware",
"statfi_search.middleware.locale.LocaleMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
# Uncomment the following if using any of the SSL settings:
# "mezzanine.core.middleware.SSLRedirectMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",
'page_types.middleware.RedirectMiddleware',
'page_types.middleware.CheckNewsDateStatus',
)
由于您使用的是 Django 2.2,您的中间件 class 必须接受 get_response 参数:
你应该这样定义你的class。
class CheckNewsDateStatus(object):
def __init__(self, get_response):
self.get_response = get_response
def process_request(self, request):
....
此外,在 Python3+ 中定义 class 时无需编写 (object)。
供您参考,documentation。
我正在尝试将 django/mezzanine 应用程序从 python 2.7 更新到 python 3.7。你能帮我修正下面的错误吗(CheckNewsDateStatus() takes no arguments
)?
好像这个class根本没用过;如果我 grep 通过所有代码,只有附加的 settings.py 和 middleware.py 匹配。它是在 django/mezzanine 中部分实现的,还是它使得整个 class 可以被删除?
我不知道代码是如何计划工作的,我也不知道该功能是否已被使用...
(python-3.7) miettinj@ramen:~/pika> python manage.py runserver
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
Watching for file changes with StatReloader
.....
_d^^^^^^^^^b_
.d'' ``b.
.p' `q.
.d' `b.
.d' `b. * Mezzanine 5.0.0
:: :: * Django 2.2
:: M E Z Z A N I N E :: * Python 3.7.10
:: :: * PostgreSQL 9.3.0
`p. .q' * Linux 5.3.18-lp152.102-default
`p. .q'
`b. .d'
`q.. ..p'
^q........p^
''''
Performing system checks...
System check identified no issues (0 silenced).
December 30, 2021 - 13:44:54
Django version 2.2, using settings 'pika.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/python/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/opt/python/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 170, in inner_run
super().inner_run(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 173, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 42, in get_internal_wsgi_application
return get_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: CheckNewsDateStatus() takes no arguments
middlewary.py:
class CheckNewsDateStatus(object):
def process_request(self, request):
if '/uutinen/' in request.path:
try:
path_to_go_raw = request.path
true_slug = path_to_go_raw.split('/uutinen/')[1:]
news_obj = Uutinen.objects.get(slug=true_slug[0])
now_utc = pytz.utc.localize(datetime.now())
hel = pytz.timezone("Europe/Helsinki")
foo = news_obj.publish_date.astimezone(hel)
if foo.date() < now_utc.date() and news_obj.status == 2:
pass
elif foo.date() == now_utc.date() and foo.time() < now_utc.time() and news_obj.status == 2:
pass
else:
if request.user.is_authenticated():
pass
elif news_obj.status == 1:
return HttpResponseNotFound('404')
else:
return HttpResponseNotFound('404')
except:
pass
settings.py:
# List of middleware classes to use. Order is important; in the request phase,
# these middleware classes will be applied in the order given, and in the
# response phase the middleware will be applied in reverse order.
MIDDLEWARE = (
'page_types.middleware.SetDynamicSite',
# 'debug_toolbar.middleware.DebugToolbarMiddleware',
"mezzanine.core.middleware.UpdateCacheMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
#"django.middleware.locale.LocaleMiddleware",
"statfi_search.middleware.locale.LocaleMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
# Uncomment the following if using any of the SSL settings:
# "mezzanine.core.middleware.SSLRedirectMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",
'page_types.middleware.RedirectMiddleware',
'page_types.middleware.CheckNewsDateStatus',
)
由于您使用的是 Django 2.2,您的中间件 class 必须接受 get_response 参数:
你应该这样定义你的class。
class CheckNewsDateStatus(object):
def __init__(self, get_response):
self.get_response = get_response
def process_request(self, request):
....
此外,在 Python3+ 中定义 class 时无需编写 (object)。
供您参考,documentation。