Thursday, January 5, 2017

Django - 010 - A quick note about the site root

As explained in the previous note, you will see a 404 error message if you view the site root - http://127.0.0.1:8000 . Django does not magically add anything to the site root; that URL is not special-cased in any way.


It is up to you to assign it to a URLpattern, just like every other entry in your URLconf. The URLpattern to match the site root is a bit un-intuitive, though, so it is worth mentioning.

When you are ready to implement a view from the site root, use the URLpattern ^$, which matches an empty string. For example:

from myD3site.views import hello, my_homepage_view
urlpatterns =[url(r'^$',my_homepage_view),]

Add the following function to views.py:

def my_homepage_view(request):
  return HttpResponse("Welcome to my Home Page")

Accessing Django root


No comments:

Post a Comment