Django 2.2破坏了以前的工作视图/ URL [英] Django 2.2 breaks previously working views/urls

查看:86
本文介绍了Django 2.2破坏了以前的工作视图/ URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



该错误似乎与我的观点有关,但是我找不到任何东西在



编辑:
@Alasdair的帮助我已经克服了这个错误,但是我仍然不知道为什么会这样。该项目可以在2.1.8上完美运行,并且文档中没有任何有关更改 handler ### 视图的方式的更改。另外,我得到 LookupError:没有安装标签为'admin'的应用程序,然后在再次卸载并安装2.2之后神奇地消失的事实也并不使我感到很高兴。 / p>

解决方案

这是一个两步问题:



首先,某些更改Django如何解析视图路径。 handler404 ='views.error_404'在2.1.8上有效,而更明确的路径 handler404 ='apps.dashboard.views.error_404'在2.2上是必需的。



第二,我在编写自定义 handler404 的方式时犯了一个错误,并且在服务器启动。 handler404 应该设置为接受两个参数 request,exception ,而我只是将其设置为接受请求



我遇到了第三个错误 LookupError:没有安装标签为 admin的应用程序最初修复路径后,但在卸载并重新安装2.2之后,我无法复制它。



感谢@Alasdair,我们得到了票证打开,看来在2.2.1。中修复该问题将很容易。



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


Decided to take out Django 2.2 for a spin (project is currently running 2.1.8) and now I can't even get the server to start. I have been maintaining this project for nearly two years and this should be a minor update.

The error seems to be related to my views, but I can't find anything in the release notes that has meaningfully changed between 2.1.8 and 2.2. I suspect it may be due to my rather non-standard view system. Instead of a single file called views.py, I use a folder of view files joined by an __init__.py. But again, that's just a guess.

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>'

Again, this view layout has been working for years now and I can't find anything relating to urls or views that has changed between 2.1.8 and 2.2.

All I am going off of is the sudden ModuleNotFoundError: No module named 'views'

Edit: With @Alasdair 's help I have gotten past this error, but I still have no idea why this is going on. The project runs perfectly on 2.1.8, and there is nothing in the docs regarding a change in how you reference handler### views. Also the fact that I got LookupError: No installed app with label 'admin' and then that magically went away after uninstalling and installing 2.2 again doesn't make me feel great.

解决方案

This is a two step problem:

First, something changed with how Django is resolving paths to views. Whereas handler404 = 'views.error_404' worked on 2.1.8, the more explicit path handler404 = 'apps.dashboard.views.error_404' is required on 2.2.

Second, I made a mistake with how I wrote my custom handler404 and a new system check caught it before the server starts. handler404 is supposed to be setup to take two arguments request, exception, whereas I had it simply setup to accept request. It may have been failing silently.

I ran into a third error LookupError: No installed app with label 'admin' after I initially fixed the path, but after uninstalling and reinstalling 2.2, I have not been able to replicate it.

Thanks to @Alasdair we've got a ticket open and it looks like this will be painless to fix in 2.2.1.

I'm simply writing this answer to summarize the complete issue, as both of the fantastic answers previously given only cover one part. Thanks guys!

这篇关于Django 2.2破坏了以前的工作视图/ URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆