URL Patterns (urls.py)
The first critical part of making a web app is figuring out how to manage the process when a client goes to a specific URL. Most of this management happens in the urls.py file in a django application. In our current application the urls.py is contained inside of the instaclone/ folder within our django project. You should open up this file with sublime as such:
Inside of this file, the critical component is the URL patterns defined as:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
This is where we will be defining our own URLs.
URL Patterns
A URL pattern is a piece of code that takes the following form:
url(urlPathMatch, urlHandler)
The overall goal of a URL pattern is the link a specific url path to a URL handler. For example, we may want certain code to run when the user goes to the "/profile" page. Thus, the urlPathMatch would correspond with "/profile" (more on this later down) and then urlHandler would be the code we wrote to run when the user goes to the profile page (more on this in the next section).
In this section we will be focusing on the urlPathMatch component. In the next session, we will be looking at URL handlers, or views as we call them.
Matching a URL
When a user goes to your website, they will type in a domain followed by a url path. For example:
- www.google.com/
- domain: www.google.com
- url path: /
- www.google.com/images/pandas
- domain: www.google.com
- url path: /images/pandas
When we are matching URLs, all we care about is the url path. Let's say that we want to handle the root page, or "/panda", url path. The way that you would do this is by defining the URL pattern as follows:
url(r'panda', ...)
Notice how when we want to match "/panda", we only have to type "panda". This is because every url path is going to have the "/" in front of it and so it just removes it as a requirement for matching. However, the issue here is that this would match "/panda", but also "/panda3d" and "/3dpanda". The reason being is that python is just looking for if "panda" is in the URL.
In order to fix this and make it so that it only matches "/panda", we have to tell python where we want "panda" to be in the URL:
url(r'^panda', ...)
By adding the "^" before the profile, it is telling django that we want the URL to start with "panda". Thus, "/3dpanda" will not match, but "/panda3d" still will. To get rid of this, we use a similar technique to tell django where the URL should end:
url(r'^panda$', ...)
The dollar sign tells django to end the URL wherever the dollar sign is. Thus, this will only match "/panda".
Matching the Homepage
Now, let's make a URL pattern for our homepage, which will be accessible at the "/" url path:
url(r'^$', ...)
This is a bit counterintuitive, but to match to the "/" url path, we have to effectively match to the empty string because django ignores the first slash, which is the entire path in this case. Thus, we tell django to start (^) and end ($) with nothing in between. Thus, only the "/" will match.
After adding the homepage URL pattern, your urls.py should look as such:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'to-be-filled-in-later')
]
Conclusion
In this section, we looked at what the urls.py is, what a URL pattern is, and how to make URL patterns. We then wrote the code necessary to handle the homepage of our website at "/".
In the next section, we are going to actually make the URL handler in the form of "views".