Skip to content

Commit d31d07d

Browse files
committed
Merge pull request #332 from HonzaKral/regex
trying to explain regexes a little more.
2 parents 34d6005 + 112d1a0 commit d31d07d

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

en/django_urls/README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,28 @@ It means that for every URL that starts with `admin/` Django will find a corresp
3737

3838
## Regex
3939

40-
Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex` -- regular expressions. Regex has a lot (a lot!) of rules that form a search pattern. It is not so easy to understand so we won't worry about it today and you'll definitely get to know them in the future. Today we will only use the ones we need.
40+
Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex` -- regular expressions. Regex has a lot (a lot!) of rules that form a search pattern. Since regexes are an advanced topic we will go in detail over how they work.
41+
42+
If you still wish to understand how we created the patterns, here is an example of the process - we will only need a limited subset of the rules to express the pattern we are looking for, namely:
43+
44+
^ for beginning of the text
45+
$ for end of text
46+
\d for a digit
47+
+ to indicate that the previous item should be repeated at least once
48+
() to capture part of the pattern
49+
50+
Anything else in the url definition will be taken literally.
51+
52+
Now imagine you have a website with the address like that: `http://www.mysite.com/post/12345/`, where `12345` is the number of your post.
53+
54+
Writing separate views for all the post numbers would be really annoying. With regular expression we can create a pattern that will match the url and extract the number for us: `^post/(\d+)/$`. Let's break it down piece by piece to see what we are doing here:
55+
56+
* **^post/** is telling Django to take anything that has `post/` at the beginning of the url (right after `^`)
57+
* **(\d+)** means that there will be a number (one or more digits) and that we want the number captured and extracted
58+
* **/** tells django that another `/` character should follow
59+
* **$** then indicates the end of the URL meaning that only strings ending with the `/` will match this pattern
60+
4161

42-
Here is a simple example just to not leave you stuck on this sentence:
43-
imagine you have a website with the address like that: `http://www.mysite.com/post/12345/`, where `12345` is the number of your post. Writing separate views for all the post numbers would be really annoying. Django makes it easier by allowing you to write `http://www.mysite.com/post/<a number>/`. Django will take care of the rest!
4462

4563
## Your first Django url!
4664

@@ -62,6 +80,9 @@ Your `mysite/urls.py` file should now look like this:
6280

6381
Django will now redirect everything that comes into 'http://127.0.0.1:8000/' to `blog.urls` and look for further instructions there.
6482

83+
When writing regular expressions in Python it is always done with `r` in front of the string - this is just a helpful hint to Python that the string may contain special characters that are not meant for Python itself but are instead part of the regular expression.
84+
85+
6586
## blog.urls
6687

6788
Create a new `blog/urls.py` empty file. All right! Add these two first lines:
@@ -77,11 +98,7 @@ After that, we can add our first URL pattern:
7798
url(r'^$', views.post_list),
7899
]
79100

80-
As you can see, we're now assigning a `view` called `post_list` to `^$` URL. But what does `^$` mean? It's a regex magic :) Let's break it down:
81-
- `^` in regex means "the beginning"; from this sign we can start looking for our pattern
82-
- `$` matches only "the end" of the string, which means that we will finish looking for our pattern here
83-
84-
If you put these two signs together, it looks like we're looking for an empty string! And that's correct, because in Django url resolvers, 'http://127.0.0.1:8000/' is not a part of URL. This pattern will show Django that `views.post_list` is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.
101+
As you can see, we're now assigning a `view` called `post_list` to `^$` URL. This regular expression will match `^` (a beginning) followed by `$` (an end) - so only an empty string will match. And that's correct, because in Django url resolvers, 'http://127.0.0.1:8000/' is not a part of URL. This pattern will show Django that `views.post_list` is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.
85102

86103
Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.
87104

0 commit comments

Comments
 (0)