Django 2.2打破以前工作的视图/网址

问题描述 投票:3回答:3

决定取出Django 2.2进行旋转(项目当前运行2.1.8),现在我甚至无法启动服务器。我一直在维护这个项目近两年,这应该是一个小小的更新。

这个错误似乎与我的观点有关,但我在release notes中找不到任何在2.1.8和2.2之间有意义变化的东西。我怀疑这可能是由于我的非标准view系统。我使用views.py连接的view文件夹,而不是一个名为__init__.py的文件。但同样,这只是猜测。

Watching for file changes with StatReloader
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python37\lib\site-packages\django\core\management\base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python37\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Python37\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 399, in check
    messages.extend(check_resolver(pattern))
  File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 400, in check
    messages.extend(self._check_custom_error_handlers())
  File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 408, in _check_custom_error_handlers
    handler, param_dict = self.resolve_error_handler(status_code)
  File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 590, in resolve_error_handler
    return get_callable(callback), {}
  File "C:\Python37\lib\site-packages\django\urls\utils.py", line 28, in get_callable
    mod = import_module(mod_name)
  File "C:\Python37\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 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'views'

Traceback (most recent call last):
  File "C:/git/si-dash/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python37\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Python37\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 95, in handle
    self.run(**options)
  File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 579, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 564, in start_django
    reloader.run(django_main_thread)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 275, in run
    self.run_loop()
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 281, in run_loop
    next(ticker)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 321, in tick
    state.update(self.loop_files(state, previous_timestamp))
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 328, in loop_files
    for path, mtime in self.snapshot_files():
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 348, in snapshot_files
    for file in self.watched_files():
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 241, in watched_files
    yield from iter_all_python_module_files()
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 128, in iter_modules_and_files
    if not path.exists():
  File "C:\Python37\lib\pathlib.py", line 1339, in exists
    self.stat()
  File "C:\Python37\lib\pathlib.py", line 1161, in stat
    return self._accessor.stat(self)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'

同样,这个视图布局已经工作多年了,我找不到任何与urlsviews有关的内容在2.1.8和2.2之间发生了变化。

我要走的只是突然的ModuleNotFoundError: No module named 'views'

Screenshot of filesystem

编辑:在@Alasdair的帮助下,我已经克服了这个错误,但我仍然不知道为什么会这样。该项目在2.1.8上完美运行,并且文档中没有关于如何引用handler###视图的更改。事实上,我得到了LookupError: No installed app with label 'admin'然后在卸载并再次安装2.2后神奇地消失了并不会让我感觉很棒。

django django-views django-urls
3个回答
2
投票

这是一个两步问题:

首先,Django如何解析视图路径的方式发生了变化。虽然handler404 = 'views.error_404'在2.1.8上工作,但2.2中需要更明确的路径handler404 = 'apps.dashboard.views.error_404'

其次,我在编写自定义handler404并在服务器启动之前捕获它的新系统检查时犯了一个错误。 handler404应该被设置为采取两个参数request, exception,而我只是设置接受request。它可能一直在默默地失败。

我在最初修复路径后遇到第三个错误LookupError: No installed app with label 'admin',但卸载并重新安装2.2后,我无法复制它。

感谢@Alasdair,我们打开了一个ticket,看起来这样在2.2.1中无法解决。

我只是写这个答案来总结完整的问题,因为之前给出的两个很棒的答案只涵盖了一个部分。多谢你们!


2
投票

回溯显示新的_check_custom_error_handlers系统检查引发错误。这表明您的urls.py中有一个无效的自定义错误处理程序,例如:

handler404 = 'views.notfound'

从项目布局的图像看,它看起来应该是这样的:

handler404 = 'apps.dashboard.views.notfound'

在Django 2.2中添加了自定义错误处理程序检查,因此当您启动runserver时,您现在会收到有关该问题的通知。在过去,Django稍后会尝试加载自定义处理程序,看起来你没注意到它无法加载。

Django 2.2.1中,有一个新的系统检查,因此当无法导入自定义错误处理程序时,您将收到更有用的错误消息。


1
投票

似乎custom error handlers是它的原因。

Django 2.1我有一个500 Error错误的自定义处理程序,如下所示:

def error_500_view(request, exception):
    return render(request,'500.html')

但是在Django 2.2中,似乎500 Error处理程序只需要1个参数,所以我改为:

def error_500_view(request):
    return render(request,'500.html')

一切都恢复正常。

因此,请确保您的404 Error处理程序类似于:

def notfound(request, exception):
    return render(request,'400.html')
© www.soinside.com 2019 - 2024. All rights reserved.