How does django generate the default page after successful installation ?

I got this question into my mind when I started learning django. After successful setup of the entire project when you hit localhost:8000/  you are prompted with the default success page that looks something like below.

  

How does django print such beautiful images?    


                             
The answer was using templates. But the very next thought that came into my mind was how does it generate it? I started exploring from the point given in image "You are seeing this image because debug is set to true".I started digging deep and kept break points. I knew that any request from the browser would be handled by WSGI Request handler so I kept a breakpoint in handle() method.
When going back in the stack trace I realized that for all the middlewares there's no problem but for the WSGIREquest : GET I got a exception which showed the tried URL's as admin/ as it is the only url present in project's url.py file.

The response_for_exception method is making a check on whether the get request is 404 and if it is 404 it checks for setting.DEBUG which is initially set to TRUE hence the technical_404_response method is called(Watch the stack trace on the left side of the above image in the Debugger window).

In the default_urlconf lies the code for rendering the default_urlconf.html which renders it from django.views.default_urlconf.html which is returned as Http Response.


Now the next question that striked my mind was what if I set DEBUG=False in settings.py ?
I experimented with the same ,I was prompted with errors to set ALLOWED HOSTS .I entered "localhost","127.0.0.1" in the list .I experimented by restarting the server and this is the output I got
I wanted to know how this output was being generated . I kept a breakpoint in get_response method
of django.core.handlers.base.py . When the execution stopped at the braekpoint I went through started using pycharm's Ctrl+B shortcut to know what functions are being called.


The above get_exception_response is used to generate exception which is then converted to html response and sent to the browser.

To be done: How does the default success page vanish whenever we add new paths or urls?

Comments

Popular Posts