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.
In theory, you can have something like :
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.
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!