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_templates/README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,32 @@
1
1
# Django templates
2
2
3
-
Time to display some data! Django gives us some helpful, built-in __template tags__ for that.
3
+
Time to display some data! Django gives us some helpful built-in __template tags__ for that.
4
4
5
5
## What are template tags?
6
6
7
-
You see, in HTML, you can't really put Python code, because browsers don't understand it. They only know HTML. We know that HTML is rather static, while Python is much more dynamic.
7
+
You see, in HTML, you can't really write Python code, because browsers don't understand it. They only know HTML. We know that HTML is rather static, while Python is much more dynamic.
8
8
9
9
__Django template tags__ allow us to transfer Python-like things into HTML, so you can build dynamic websites faster and easier. Yikes!
10
10
11
11
## Display post list template
12
12
13
13
In the previous chapter we gave our template a list of posts in the `posts` variable. Now we will display it in HTML.
14
14
15
-
To print a variable in Django template, we use double curly brackets with the variable's name inside, like this:
15
+
To print a variable in Django templates, we use double curly brackets with the variable's name inside, like this:
16
16
17
17
```html
18
18
{{ posts }}
19
19
```
20
20
21
-
Try this in your `blog/templates/blog/post_list.html` template (replace everything from the second `<div>` to the third `</div>` with `{{ posts }}`), save the file and refresh the page to see the results:
21
+
Try this in your `blog/templates/blog/post_list.html` template. Replace everything from the second `<div>` to the third `</div>` with `{{ posts }}`. Save the file, and refresh the page to see the results:
22
22
23
23

24
24
25
25
As you can see, all we've got is this:
26
26
27
27
[<Post: My second post>, <Post: My first post>]
28
28
29
-
This means that Django understands it as a list of objects. Remember from __Introduction to Python__ how we can display lists? Yes, with the for loops! In a Django template, you do them this way:
29
+
This means that Django understands it as a list of objects. Remember from __Introduction to Python__ how we can display lists? Yes, with for loops! In a Django template you do them like this:
Copy file name to clipboardExpand all lines: en/django_urls/README.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Django urls
2
2
3
-
We're about to build our first webpage -- a homepage for your blog! But first, let's learn a little bit about Django urls.
3
+
We're about to build our first webpage: a homepage for your blog! But first, let's learn a little bit about Django urls.
4
4
5
5
## What is a URL?
6
6
7
-
A URL is simply a web address, you can see a URL every time you visit any website - it is visible in your browser's address bar (yes! `127.0.0.1:8000` is a URL! And http://djangogirls.com is also a URL):
7
+
A URL is simply a web address. You can see a URL every time you visit a website - it is visible in your browser's address bar (yes! `127.0.0.1:8000` is a URL! And `https://djangogirls.com` is also a URL):
8
8
9
9

10
10
11
-
Every page on the Internet needs its own URL. This way your application knows what it should show to a user who opens a URL. In Django we use something called `URLconf` (URL configuration), which is a set of patterns that Django will try to match with the received URL to find the correct view.
11
+
Every page on the Internet needs its own URL. This way your application knows what it should show to a user who opens a URL. In Django we use something called `URLconf` (URL configuration). URLconf is a set of patterns that Django will try to match with the received URL to find the correct view.
12
12
13
13
## How do URLs work in Django?
14
14
@@ -41,7 +41,7 @@ It means that for every URL that starts with `admin/` Django will find a corresp
41
41
42
42
## Regex
43
43
44
-
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 not go in detail over how they work.
44
+
Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex`, short for "regular expressions". Regex has a lot (a lot!) of rules that form a search pattern. Since regexes are an advanced topic, we will not go in detail over how they work.
45
45
46
46
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:
47
47
@@ -63,7 +63,6 @@ Writing separate views for all the post numbers would be really annoying. With r
63
63
***$** then indicates the end of the URL meaning that only strings ending with the `/` will match this pattern
64
64
65
65
66
-
67
66
## Your first Django url!
68
67
69
68
Time to create our first URL! We want 'http://127.0.0.1:8000/' to be a homepage of our blog and display a list of posts.
@@ -86,7 +85,7 @@ urlpatterns = [
86
85
87
86
Django will now redirect everything that comes into 'http://127.0.0.1:8000/' to `blog.urls` and look for further instructions there.
88
87
89
-
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.
88
+
When writing regular expressions in Python it is always done with `r` in front of the string. This is a helpful hint for Python that the string may contain special characters that are not meant for Python itself, but for the regular expression instead.
90
89
91
90
92
91
## blog.urls
@@ -108,16 +107,16 @@ urlpatterns = [
108
107
]
109
108
```
110
109
111
-
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.
110
+
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. That's correct, because in Django URL resolvers, 'http://127.0.0.1:8000/' is not a part of the URL. This pattern will tell 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.
112
111
113
-
The last part `name='post_list'` is the name of the URL that will be used to identify the view. This can be the same as the name of the view but it can also be something completely different. We will be using the named URLs later in the project but it is important to name each URL in the app and to try to keep the names of URLs unique and easy to remember.
112
+
The last part `name='post_list'` is the name of the URL that will be used to identify the view. This can be the same as the name of the view but it can also be something completely different. We will be using the named URLs later in the project so it is important to name each URL in the app. We should also try to keep the names of URLs unique and easy to remember.
114
113
115
114
Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.
116
115
117
116

118
117
119
118
There is no "It works" anymore, huh? Don't worry, it's just an error page, nothing to be scared of! They're actually pretty useful:
120
119
121
-
You can read that there is __no attribute 'post_list'__. Is *post_list* reminding you of anything? This is how we called our view! This means that everything is in place, we just didn't create our *view* yet. No worries, we will get there.
120
+
You can read that there is __no attribute 'post_list'__. Is *post_list* reminding you of anything? This is what we called our view! This means that everything is in place but we just haven't created our *view* yet. No worries, we will get there.
122
121
123
122
> If you want to know more about Django URLconfs, look at the official documentation: https://docs.djangoproject.com/en/1.8/topics/http/urls/
Copy file name to clipboardExpand all lines: en/django_views/README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Time to get rid of the bug we created in the last chapter :)
4
4
5
-
A *view* is a place where we put the "logic" of our application. It will request information from the `model` you created before and pass it to a `template` that you will create in the next chapter. Views are just Python methods that are a little bit more complicated than the thing we did in the __Introduction to Python__ chapter.
5
+
A *view* is a place where we put the "logic" of our application. It will request information from the `model` you created before and pass it to a `template`. We'll create a template in the next chapter. Views are just Python methods that are a little bit more complicated than the ones we wrote in the __Introduction to Python__ chapter.
6
6
7
7
Views are placed in the `views.py` file. We will add our *views* to the `blog/views.py` file.
8
8
@@ -27,7 +27,7 @@ def post_list(request):
27
27
28
28
As you can see, we created a method (`def`) called `post_list` that takes `request` and `return` a method `render` that will render (put together) our template `blog/post_list.html`.
29
29
30
-
Save the file, go to http://127.0.0.1:8000/ and see what we have got now.
30
+
Save the file, go to http://127.0.0.1:8000/ and see what we have got.
0 commit comments