问题
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.
回答1:
It seems the custom error handlers
are the cause of it.
In Django 2.1
I had a custom handler for a 500 Error
error like this:
def error_500_view(request, exception):
return render(request,'500.html')
But in Django 2.2
it seems that the 500 Error
handler only takes 1 argument, so I changed to:
def error_500_view(request):
return render(request,'500.html')
And everything is working normal again.
So make sure your 404 Error
handler is something like:
def notfound(request, exception):
return render(request,'400.html')
回答2:
The traceback shows that the new _check_custom_error_handlers system check is raising an error. This suggests that you have an invalid custom error handler in your urls.py
, e.g.
handler404 = 'views.notfound'
From image of your project layout, it looks as if that should be something like:
handler404 = 'apps.dashboard.views.notfound'
The custom error handler check was added in Django 2.2, so you are now notified about the problem when you start runserver
. In the past, Django would have tried to load the custom handler later, and it looks like you didn't notice that it failed to load.
In Django 2.2.1, there is a new system check, so you will get a more useful error message when the custom error handler cannot be imported.
回答3:
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!
回答4:
Another thing is handler(request, *args)
, but essentially it's the same as what is suggested above.
来源:https://stackoverflow.com/questions/55481810/django-2-2-breaks-previously-working-views-urls