You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/django_urls/README.md
+25-8Lines changed: 25 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,10 +37,28 @@ It means that for every URL that starts with `admin/` Django will find a corresp
37
37
38
38
## Regex
39
39
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
+
41
61
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!
44
62
45
63
## Your first Django url!
46
64
@@ -62,6 +80,9 @@ Your `mysite/urls.py` file should now look like this:
62
80
63
81
Django will now redirect everything that comes into 'http://127.0.0.1:8000/' to `blog.urls` and look for further instructions there.
64
82
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
+
65
86
## blog.urls
66
87
67
88
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:
77
98
url(r'^$', views.post_list),
78
99
]
79
100
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.
85
102
86
103
Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.
0 commit comments