Skip to content

Commit a15e620

Browse files
committed
Merge pull request #443 from phalt/hemingway
Put django urls, django view, and django templates chapter through hemingway
2 parents 73da2ef + fea0c1c commit a15e620

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

en/django_templates/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# Django templates
22

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.
44

55
## What are template tags?
66

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.
88

99
__Django template tags__ allow us to transfer Python-like things into HTML, so you can build dynamic websites faster and easier. Yikes!
1010

1111
## Display post list template
1212

1313
In the previous chapter we gave our template a list of posts in the `posts` variable. Now we will display it in HTML.
1414

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:
1616

1717
```html
1818
{{ posts }}
1919
```
2020

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:
2222

2323
![Figure 13.1](images/step1.png)
2424

2525
As you can see, all we've got is this:
2626

2727
[<Post: My second post>, <Post: My first post>]
2828

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:
3030

3131
```html
3232
{% for post in posts %}

en/django_urls/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Django urls
22

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.
44

55
## What is a URL?
66

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):
88

99
![Url](images/url.png)
1010

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.
1212

1313
## How do URLs work in Django?
1414

@@ -41,7 +41,7 @@ It means that for every URL that starts with `admin/` Django will find a corresp
4141

4242
## Regex
4343

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.
4545

4646
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:
4747

@@ -63,7 +63,6 @@ Writing separate views for all the post numbers would be really annoying. With r
6363
* **$** then indicates the end of the URL meaning that only strings ending with the `/` will match this pattern
6464

6565

66-
6766
## Your first Django url!
6867

6968
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 = [
8685

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

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.
9089

9190

9291
## blog.urls
@@ -108,16 +107,16 @@ urlpatterns = [
108107
]
109108
```
110109

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.
112111

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.
114113

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

117116
![Error](images/error1.png)
118117

119118
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:
120119

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.
122121

123122
> If you want to know more about Django URLconfs, look at the official documentation: https://docs.djangoproject.com/en/1.8/topics/http/urls/

en/django_views/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Time to get rid of the bug we created in the last chapter :)
44

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.
66

77
Views are placed in the `views.py` file. We will add our *views* to the `blog/views.py` file.
88

@@ -27,7 +27,7 @@ def post_list(request):
2727

2828
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`.
2929

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.
3131

3232
Another error! Read what's going on now:
3333

0 commit comments

Comments
 (0)