Django uses regular expressions to determine which view a request should be handled by. This video teaches regular expressions and shows some examples of patterns you're likely to use for URL patterns.
- [Narrator] Let's dig a bit deeper into URL patterns. At a high level, URL patterns dispatch requests to views based on the path. To begin, let's take a brief look at regular expressions. URL patterns use regular expressions to interpret the URLs for your site. A regular expression is a sequence of characters that defines a search pattern for strings. Here's some examples for some regular expressions that we'll use. With the regular expression ducky, this will match the word ducky anywhere found in the string.
With \d this will match a single digit character. This has nothing to do with the literal character d itself. D here just stands for digit and the backslash means that it will be interpreted to mean any digit character. \d+ means one or more digit characters so this will match any number of characters up to the point where something other than a number is encountered. So if the string is 12 ounces, this will match against the characters 1 and 2. The caret character means beginning of sequence so the regular expression ^admin/ will match any string that begins with admin/ but will not match the word admin anywhere else in the string.
Similarly, the dollar sign means end of sequence. So suffix $ will match any string that ends with the word suffix. The regular expression ^$ will match any empty string because it begins and ends with nothing in between. At this point, we've discussed regular expressions enough to get comfortable with using them in URL patterns. However, to learn more, I'd invite you to check out courses on regular expressions in our course library. Also, as we learn about using regular expressions in Python, I invite you to use the tool pythex.org to play with different regular expressions.
Here, I'll go to the input box labeled your regular expression and I'll type \d+ and then in the input box labeled your test string, I'll type 12 ounces. Now at the bottom of the screen in the results, I can see that the characters 1 and 2 matched because they're highlighted while the rest of the string did not match. Now, let's look at some example URL patterns. The urls.pie file will define a variable called URL patterns which is a list of calls to the URL function.
The URL function has three parameters. The first parameter is a regular expression. This will be used to match against the request. In this example, we're using ^$ so we're matching against the empty string which is a request to our site with no path. Django doesn't consider the final slash in a URL for matching so even though the route path is really a slash, it is treated as an empty string. Also, you might notice that our regular expression is preceded by an r character. This tells Python that we want to interpret this string as being a raw string.
In Python, a raw string doesn't use backslash as an escape character which is the normal behavior for Python strings. Regular expressions often include a backslash so it's easy if by convention we just always make our strings raw rather they have backslashes or not. So, for regular expressions, most Python developers just always make their regular expression use a raw string. The second argument is the view to call if there's a match. In this example, if someone comes to the root path of our site, it will go to the home view. If we follow the path of a request through our project with this URL pattern defined, a request to the root path will be directed to the home view which, once implemented, will leverage the adoptions/home.html template.
Lastly, the URL patterns take a name parameter at the end. This will be useful inside of templates which we'll look at later. Let's look at the overall procedure used by URL patterns. It begins at the top of the list and determines if the first one matches. If there's a match, it will call the associated view with the request. Otherwise, it will continue into the next pattern. In this case, if the URL is slash, then we'll go to the home view. Otherwise, it will check the next pattern. If there are no matches to any patterns, Django will return a 404 page.
Now that we've seen the concepts behind URL patterns, we're ready to implement them for our project.
- Creating a new Django project
- Defining Django models and fields
- Performing Django migrations
- Querying data with the Django ORM
- Building URL handlers and views
- Building Django templates