Opened 15 months ago
Closed 15 months ago
#35703 closed Bug (fixed)
Default URLconf detection does not take a prefix into account
| Reported by: | Maarten Breddels | Owned by: | Maarten Breddels | 
|---|---|---|---|
| Component: | Error reporting | Version: | 5.1 | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
Hi,
When Django runs onder a prefix (often described by SCRIPT_NAME for cgi/wsgi or root_path in asgi land) the default URLconf is not detected.
The default page shown is then:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/_app/m05jjd2cn5gycxqcowk/ Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: admin/ The empty path didn’t match any of these. You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
(in this case, the asgi root_path is /_app/m05jjd2cn5gycxqcowk)
While instead, I expected to see the nice default page with
The install worked successfully! Congratulations!
The bug was introduced in https://github.com/maartenbreddels/django/commit/0ecb9f6e2514cfd26a678a280d471433375101a3
which uses request.path == '/' for comparison, while request.path_info == '/' was probably the intent. This commit was part of https://github.com/maartenbreddels/django/commit/3f1c7b70537330435e2ec2fca9550f7b7fa4372e
Note that request.path includes the prefix (SCRIPT_NAME or root_path), while request.path does not.
I hit this bug when trying to run Django on https://py.cafe, a platform that can run web applications on Pyodide.
The project at:
https://py.cafe/maartenbreddels/django-start-template (which includes this patch)
now runs fine because I monkey-patched it (see https://py.cafe/files/maartenbreddels/django-start-template/django_patch.py), showing this is a correct fix.
For technical reasons, we need to run under a prefix (we configure root_path in the asgi scope), and django was giving me a 404. This gave the (false) impression django did not support running under a prefix (StackOverflow falsely confirmed this suspicion). 
I already opened a PR at https://github.com/django/django/pull/18505 showing the change required. I'm happy to reopen that PR and do minor work on it, but I don't think I'll have the bandwidth to add a test.
Regards,
Maarten Breddels
Change History (4)
comment:1 by , 15 months ago
| Component: | Uncategorized → Error reporting | 
|---|---|
| Has patch: | set | 
| Patch needs improvement: | set | 
| Summary: | default URLconf detection does not take a prefix into account → Default URLconf detection does not take a prefix into account | 
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 15 months ago
| Owner: | set to | 
|---|---|
| Patch needs improvement: | unset | 
| Status: | new → assigned | 
comment:3 by , 15 months ago
| Triage Stage: | Accepted → Ready for checkin | 
|---|
Hi Maarten, thank you for the report!
I have written a test which you should be able to use
tests/view_tests/tests/test_debug.py