Opened 4 weeks ago

Closed 4 weeks 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 Sarah Boyce, 4 weeks ago

Component: UncategorizedError reporting
Has patch: set
Patch needs improvement: set
Summary: default URLconf detection does not take a prefix into accountDefault URLconf detection does not take a prefix into account
Triage Stage: UnreviewedAccepted

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

    a b class DebugViewTests(SimpleTestCase):  
    398398            response, "<h1>The install worked successfully! Congratulations!</h1>"
    399399        )
    400400
     401    @override_settings(ROOT_URLCONF="view_tests.default_urls", FORCE_SCRIPT_NAME="/FORCED_PREFIX")
     402    def test_default_urlconf_script_name(self):
     403        response = self.client.request(**{"path": "/FORCED_PREFIX/"})
     404        self.assertContains(
     405            response, "<h1>The install worked successfully! Congratulations!</h1>"
     406        )
     407
    401408    @override_settings(ROOT_URLCONF="view_tests.regression_21530_urls")

comment:2 by Sarah Boyce, 4 weeks ago

Owner: set to Maarten Breddels
Patch needs improvement: unset
Status: newassigned

comment:3 by Sarah Boyce, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:4 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In cdcd604e:

Fixed #35703 -- Made technical_404_response() respect SCRIPT_NAME to return default_urlconf().

Note: See TracTickets for help on using tickets.
Back to Top