Baby talk about Django urls

Akash N O
2 min readJul 5, 2021

--

Guide for the web site

As the title suggests URLs are simply a guide for our website. Django URLs will navigate you around the website based on the URL you type on the search bar. Let’s imagine we are building the Facebook website in Django(hope mark doesn’t know about that😄).

urls.py

So when a users type facebook.com/friends on the browser, our Django URL function takes that and sends back the Facebook page which contains the friends list view. Now if you type facebook.com/about, the URL function sends back the Facebook about page view

Now lets create a url to navigate to home page when the user type only facebook.com

urls.py

You can see that we specified an empty string so that Django will know to return home view when the user types just the domain name(that is facebook.com).

Now you may be thinking what will happen if user types something which is not defined on the urls.py file(like facebook.com/hoorey). Then Django will return a 404 error not found Html page to the user

404.html

Structure of urls.py

Now lets see the Best practice of setting up Django urls.py file. We can create a urls.py file inside the app directory and include that in our project urls.py Here i have created a urls.py file and inside that you can see the url patterns and connected views

urls.py

Now all we have to do is to include the newly created urls.py to the project’s urls.py using include function

--

--

Akash N O
Akash N O

No responses yet