Django 2.2破坏了以前的工作视图/ URL

时间:2019-04-02 18:59:32

标签: django django-views django-urls

决定试用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之后神奇地消失的事实也并不会让我感觉很好。

4 个答案:

答案 0 :(得分: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 :(得分:2)

这是一个两步问题:

首先,随着Django解决视图路径的方式发生了一些变化。 handler404 = 'views.error_404'在2.1.8上工作,而更明确的路径handler404 = 'apps.dashboard.views.error_404'在2.2上是必需的。

第二,我在编写自定义handler404的方式时犯了一个错误,并且在服务器启动之前,新的系统检查发现了它。 handler404应该被设置为接受两个参数request, exception,而我只是简单地将其设置为接受request。它可能一直在默默地失败。

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

感谢@Alasdair,我们已经开放了ticket,看起来可以轻松修复到2.2.1中。

我只是写这个答案来总结整个问题,因为先前给出的两个奇妙的答案都只涵盖了一部分。谢谢大家!

答案 2 :(得分: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')

答案 3 :(得分:0)

另一件事是handler(request, *args),但本质上与上面建议的相同。