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

Jon*_*Jon 5 django django-urls django-views

决定尝试使用Django 2.2(项目当前正在运行2.1.8),现在我什至无法启动服务器。我已经将这个项目维持了将近两年,这应该是一个较小的更新。

该错误似乎与我的观点有关,但是我在发行说明中找不到在2.1.8和2.2之间有有意义的更改的任何内容。我怀疑这可能是由于我的非标准view系统造成的。views.py我使用一个由view.NET连接的文件文件夹,而不是一个单独的文件__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>'
Run Code Online (Sandbox Code Playgroud)

同样,这种视图布局已经使用了多年了,我找不到与2.1.8和2.2之间相关的内容urlsviews在2.1.8和2.2之间发生过更改的任何内容。

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

文件系统的屏幕截图

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

dre*_*c4s 8

看来 custom error handlers是的原因。

Django 2.1我有一个自定义处理程序来处理这样的500 Error错误:

def error_500_view(request, exception):
    return render(request,'500.html')
Run Code Online (Sandbox Code Playgroud)

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

def error_500_view(request):
    return render(request,'500.html')
Run Code Online (Sandbox Code Playgroud)

一切都恢复正常了。

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

def notfound(request, exception):
    return render(request,'400.html')
Run Code Online (Sandbox Code Playgroud)

  • 500 处理程序在 [2.1](https://docs.djangoproject.com/en/2.1/ref/views/#django.views.defaults.server_error) 和 [2.2] 中都采用了 `request` 和可选的 `template_name` (https://docs.djangoproject.com/en/2.2/ref/views/#django.views.defaults.server_error),它没有改变。Django 2.2 中的新检查已确定您的处理程序具有错误的签名。 (2认同)

Jon*_*Jon 2

这是一个两步问题:

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

其次,我在编写自定义内容时犯了一个错误handler404,新的系统检查在服务器启动之前发现了它。handler404应该设置为接受两个参数request, exception,而我只是将其设置为接受request。它可能已经悄然失败了。

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

感谢@Alasdair,我们已经收到了一张,看起来这个问题在 2.2.1 中可以轻松修复。

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