A URL is a uniform resource locator. It contains the address that links to a resource such as an HTML page.
For example, “https://henrydangprg.com” is a URL, that links to the HTML page that contains this website. A single website, like this, can have many other URL’s formed by adding a backslash (“/”) after the domain name.
If you wanted to access the about page on this website, you would add “/about/” to the end of the home page’s URL. It can be visualized like a tree.
You start with the base website, and you have other possible URL’s accessible by adding a backslash and some word.
- https://henrydangprg.com/
- about/
- contact/
- infinitely-many-other-possibilities/
- which-can-contain-other-links/
- containing-potentially-even-more-links/
- which-can-contain-other-links/
In theory, you can have something like :
https://henrydangprg.com/foo/bar/foobar/foobarbar/foofoo-ad-infinitum/
How Does It Work in Django?
In Django, the premise is exactly the same. Inside each project is a urls.py file.
You’ll see something along the lines of this :
from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), ]
Here, you can see that Django handles the URL routing with regular expressions. The ‘r’ before a string indicates that the following string is raw input. This means that Python will not convert things like ‘\n’ into a new line, and will instead process it as is.
You can play around with the urlpatterns list, and add new url’s to test that it works.
For example, if we change it to :
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^foobar/', admin.site.urls), ]
Then foobar will become a possible extension to your website’s URL. Assuming you’re using localhost and a port of 8000, it would be 127.0.0.1:8000/foobar/ to access your new URL.
If you want to go down two layers deep, like 127.0.0.1:8000/foobar/foo, you should create a new app.
Let’s make a new app called “foobar”.
django-admin startapp foobar
Modify the urlpatterns list inside your project’s urls.py file. We are now going to add foobar’s urls.py file into the project’s urls.py.
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^foobar/', include('foobar.urls')), url(r'^admin/', admin.site.urls), ]
Essentially, any time we visit 127.0.0.1:8000/foobar/, Django will see that we want to access “foobar”, and will check foobar’s urls.py for the next portion of the URL.
Now, we have to add add URLs into foobar. Go into the foobar directory and modify urls.py.
from django.conf.urls import url from django.contrib import admin from . import views urlpatterns = [ url(r'^$/', admin.site.urls), url(r'^foo/', admin.site.urls), ]
I don’t suggest you actually make every URL link to admin.site.urls, but for the sake of simplicity, we will stick to using that.
The ‘^$’ simply indicates that if there is nothing, then load the admin page. In this case, it would be 127.0.0.1:8000/foobar/ because there is nothing after “foobar/”, which is what our project’s urls.py looked up until.
We also added “foo” to our urlpatterns, which means we can now visit 127.0.0.1:8000/foobar/foo, allowing us to add extensions to our URL.
You can add virtually any URL you want, and as many levels of URLS as needed. You don’t even have to add new apps for each new extension. However, you will have to write a lot of duplicated code if you do that.
#With new apps urlpatterns = [ url(r'^foobar/', include('foobar.urls')), url(r'^admin/', admin.site.urls), ] #foobar.urls urlpatterns = [ url(r'^$/', admin.site.urls), url(r'^foo/', admin.site.urls), ] #Without new apps, anti-DRY urlpatterns = [ url(r'^foobar/$', admin.site.urls), url(r'^foobar/foo$', admin.site.urls) url(r'^admin/$', admin.site.urls), ]
You can see that you would have to write out the full URL each time. If you had 100 URL’s, all 100 would have to be crammed into this single urlpatterns list, and if it goes 10 layers deep, you would have to write it all out each time.
Conclusion
URL routing with Django is absurdly easy, provided that you know a little bit of regex. However, if you are struggling with regular expressions, you can click here for an interactive regular expressions tester.
Best of luck with learning Django, and happy coding!
I found this really helpful and finally had that eureka moment I’ve been waiting for, thank you Henry.
One thing I don’t quite get is why it’s better to create a brand new app for each additional url link. I’m thinking that because creating a new app seems to create a whole directory structure, simply typing out the url actually seems more efficient: one line vs. a whole new directory. Is there another way to look at this?
LikeLike
It’s a good idea to create a new app because having all of the URLs in the exact same file is a bad idea. Adding that one extra line might be fine if you only have two or three URLs, but for more complex web pages, you could potentially have upwards of hundreds or thousands of URLs. Putting all of those into one file, and trying to debug it when something goes wrong, is an absolute nightmare.
On the other hand, if you make a new app for it, it’ll be clear where you have to look when a problem arises, because all of the URLs will be partitioned off by a separate app. So the benefits are really two-fold, to clearly separate the URLs, and to make debugging and testing a lot easier in the long run if you later have a lot (hundreds/thousands) of URLs on your webpage.
LikeLiked by 1 person