[Solved]-Who generates the default page of django welcome page?

4👍

Take a look at django/core/handlers/base.py and django/views/debug.py. In a nutshell, if django gets a 404, which it will if you don’t have any routes set up, then in base.py

if settings.DEBUG:
    from django.views import debug
    response = debug.technical_404_response(request, e)

And in debug.py look at technical_404_response and empty_urlconf

👤sberry

14👍

If anyone would like to have it back (or reuse it), you just need to add debug.default_urlconf view to your urls:

…
from django.views import debug
…

urlpatterns = [
    …
    path('', debug.default_urlconf),
    …
]
👤maciek

8👍

If your urls.py is empty (as in contains no patterns to match urls) and Django is in debug mode (DEBUG = True in your settings) then Django fires back the page you’re seeing.

The Django view:

https://github.com/django/django/blob/main/django/views/debug.py#L575-L583

The HTML template:

https://github.com/django/django/blob/ca9872905559026af82000e46cde6f7dedc897b6/django/views/templates/default_urlconf.html

👤Matt

5👍

(Disclaimer: I am using django version: 3.2.5, In older versions the file content may vary)

The default landing page that you see when you run your django web app for first time is "default_urlconf.html". It’s a fall safe mechanism added by django rather than giving 404 error in absence of any default url/routes.

When you first create a django website using commands like django-admin startproject <project_name> or using any IDE menu control like in case of "Pycharm professionals", you get the following folder structure. Also, the Debug is set as True (DEBUG = True) in your setting.py file

enter image description here

Additionally,
when the route is not set or the url maps are empty in url.py then the django framework provides default route through its error handling code.
It’s like when you try to access/request http://127.0.0.1:8000/ django checks if that url exist or not(404) and also if the debug mode is active. This request is traversed through various *.py files like base.py ,handlers.py, exceptions.py, and finally it goes to debug.py. All these files are provided with virtual environment or the moment you (install django in your project).

Eventually, From "exception.py" it goes to "debug.py" through method **debug.technical_404_response(request, exc)**

def response_for_exception(request, exc):
    if isinstance(exc, Http404):
        if settings.DEBUG:
            response = debug.technical_404_response(request, exc)
        else:
            response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)

finally it sense that debug.py has def technical_404_response(request, exception) which calls def default_urlconf(request) and eventually it returns the response of a default html page (default_urlconf.html) that you see on the screen

def default_urlconf(request):
    """Create an empty URLconf 404 error response."""
    with Path(CURRENT_DIR, 'templates', 'default_urlconf.html').open(encoding='utf-8') as fh:
        t = DEBUG_ENGINE.from_string(fh.read())
    c = Context({
        'version': get_docs_version(),
    })

    return HttpResponse(t.render(c), content_type='text/html')

enter image description here

location of files:

  • exception.py:
    venv/Lib/site-packages/django/core/handlers/exception.py

  • debug.py: venv/Lib/site-packages/django/views/debug.py

  • default_urlconf.html :
    venv/Lib/site-packages/django/views/templates/default_urlconf.html

👤Sunny

Leave a comment