Skip to content

Commit

Permalink
Proofreading
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Edgy Edgecombe committed Jul 26, 2014
1 parent 4d40c43 commit 9fffb99
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
14 changes: 7 additions & 7 deletions django_orm/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Django Querysets

We have different pieces in place: the `Post` model is defined in `models.py`, we have `post_list` in `views.py` and the template added. But how will we actually make our posts appear in our HTML template? Because that is what we want: take a content (models saved in the database) and display it nicely in our template, right?
We have different pieces in place: the `Post` model is defined in `models.py`, we have `post_list` in `views.py` and the template added. But how will we actually make our posts appear in our HTML template? Because that is what we want: take some content (models saved in the database) and display it nicely in our template, right?

This is exactly what *views* are supposed to do: connect models and templates. In our `post_list` *view* we will need to take models we want to display and pass them to the template. So basically in a *view* we decide what (model) will be displayed in a template.

Expand Down Expand Up @@ -30,15 +30,15 @@ So now we are interested in a list of blog posts, right? But all we have is mode

which returns all blog posts!

And we have our first queryset! Now we can take each element of it and display it or do something else with it.
And we have our first queryset! Now we can take each element out of it and display it or do something else with it.

But before we will pass this queryset to the template and display blog posts we will do some magic and we will select only posts that are published (they have a `published_date` set).
But before we pass this queryset to the template and display blog posts we will do some magic and select only posts that are published (they have a `published_date` set).

Here comes a `filter`. We will use it instead of `all` in a previous line of code. In parentheses we will state what condition needs to be met by a blog post to end up in our queryset. In our situation it is `published_date` that is not empty. The way to write it in Django is: `published_date__isnull=False` (`null` in programming means *empty*).
This is where we introduce a `filter`. We will use it instead of `all` in a previous line of code. In parentheses we will state what condition(s) needs to be met by a blog post to end up in our queryset. In our situation it is `published_date` that is not empty. The way to write it in Django is: `published_date__isnull=False` (`null` in programming means *empty*).

Post.objects.filter(published_date__isnull=False)

Finally, it would be good to have our posts ordered by publish date, right? We can do that with `order_by`. In parentheses we will type (in quotation marks `''`) a name of the field (`published_date`). Our final queryset looks like this:
Finally, it would be good to have our posts ordered by publish date, right? We can do that with `order_by`. In parentheses we will type (in quotation marks `''`) the name of a field (`published_date`) to order by. Our final queryset looks like this:

Post.objects.filter(published_date__isnull=False).order_by('published_date')

Expand All @@ -51,10 +51,10 @@ Time to put this piece of code inside `post_list`, right?
posts = Post.objects.filter(published_date__isnull=False).order_by('published_date')
return render(request, 'blog/post_list.html', {})

Please note that we create a *variable* for our queryset: `posts`. Treat it as a name of our queryset. From now on we can refer to it by this name.
Please note that we create a *variable* for our queryset: `posts`. Treat this as the name of our queryset. From now on we can refer to it by this name.
The last missing part is to pass the `posts` queryset to the template (we will cover how to display it in a next chapter).

In the `render` function we already have parameter with `request` (so everything we receive from the user via the Internet) and a template file `'blog/post_list.html'`. The last parameter, which looks like this: `{}` is a place in which we can add some things to template. We need to give them names (we will stick to `'posts'` right now :)). It should look like this: `{'posts': posts}`. Please note that the part before `:` is wrapped with quotes `''`.
In the `render` function we already have parameter with `request` (so everything we receive from the user via the Internet) and a template file `'blog/post_list.html'`. The last parameter, which looks like this: `{}` is a place in which we can add some things for the template to use. We need to give them names (we will stick to `'posts'` right now :)). It should look like this: `{'posts': posts}`. Please note that the part before `:` is wrapped with quotes `''`.

So finally our `blog/views.py` file should look like this:

Expand Down
12 changes: 6 additions & 6 deletions django_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ __Django template tags__ allow us to transfer Python-like things into HTML, so y

## Display post list template

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

To print a variable in Django template, we use double curly brackets with the variable's name inside, like this:

{{ posts }}

Try this in your `blog/templates/blog/post_list.html` template (replace everything between a second `<div></div>` tags with `{{ posts }}` line), save the file and refresh the page to see the results:
Try this in your `blog/templates/blog/post_list.html` template (replace everything between the second `<div></div>` tags with `{{ posts }}` line), save the file and refresh the page to see the results:

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

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

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

This means that Django understand it as a list of objects. Remember from __Introduction to Python__ how we can display lists? Yes, with for loops! In Django template, you do them this way:
This means that Django understand 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 this way:

{% for post in posts %}
{{ post }}
Expand All @@ -34,7 +34,7 @@ Try this in your template.

![Figure 13.2](images/step2.png)

It works! But we want them to be displayed in a way we created earlier in __Introduction to HTML__ chapter - like the static posts we put there before. You can mix HTML and template tags. Our `body` will look like that:
It works! But we want them to be displayed in a way we created earlier in the __Introduction to HTML__ chapter - like the static posts we put there before. You can mix HTML and template tags. Our `body` will look like this:

<div>
<h1><a href="/">Django Girls Blog</a></h1>
Expand All @@ -52,9 +52,9 @@ Everything you put between `{% for %}` and `{% endfor %}` will be repeated for e

![Figure 13.3](images/step3.png)

Have you noticed that we used sligthly different notation this time `{{ post.title }}` or `{{ post.text }}`. We are accessing data in each of fields defined in our `Post` model.
Have you noticed that we used a slightly different notation this time `{{ post.title }}` or `{{ post.text }}`. We are accessing data in each of the fields defined in our `Post` model.

Congrats! Now go ahead and try adding a new post in your Django admin (remember to add published_date!), then refresh your page to see if the post appeared there.
Congrats! Now go ahead and try adding a new post in your Django admin (remember to add published_date!), then refresh your page to see if the post appears there.

Works like a charm? We're proud! Treat yourself something sweet, you have earned it :)

Expand Down
26 changes: 13 additions & 13 deletions extend_your_application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ We've already completed all the different steps necessary for the creation of ou

Time to practice!

First thing neccesary in our blog is obviously a page that display one post, right?
The first thing we need in our blog is, obviously, a page to display one post, right?

We already have a `Post` model, so we don't need to add anything to `models.py`.

Expand All @@ -28,23 +28,23 @@ We want to have a link to a post detail page on the post's title. Let's change `

<h1><a href="{% url 'blog.views.post_detail' pk=post.pk %}">{{ post.title }}</a></h1>

Time to explain mysterious `{% url 'blog.views.post_detail' pk=post.pk %}`. As you suspect `{% %}` notation means that we are using Django template tags. This time we will use one that will create an URL for us!
Time to explain the mysterious `{% url 'blog.views.post_detail' pk=post.pk %}`. As you suspect `{% %}` notation means that we are using Django template tags. This time we will use one that will create a URL for us!

`blog.views.post_detail` is a path to a `post_detail` *view* we want to create. Please note: `blog` is the name of our application (in folder `blog`), `views` is from the name of the `views.py` file and the last bit: `post_detail` is the name of the *view*.

Now when we go to:

http://127.0.0.1:8000/

we will have an error (as suspected, since we don't have an URL or a *view* for `post_detail`). It will look like this:
we will have an error (as suspected, since we don't have a URL or a *view* for `post_detail`). It will look like this:

![NoReverseMatch error](images/no_reverse_match2.png)

Let's create an URL in `urls.py` for our `post_detail` *view*!
Let's create a URL in `urls.py` for our `post_detail` *view*!

### URL: http://127.0.0.1:8000/post/1/

We want to create an URL to point Django to a *view* called `post_detail`, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),` to the `blog/urls.py` file. It should look like this:
We want to create a URL to point Django to a *view* called `post_detail`, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),` to the `blog/urls.py` file. It should look like this:

from django.conf.urls import patterns, include, url
from . import views
Expand All @@ -61,9 +61,9 @@ That one looks scary, but no worries - we will explain it for you:
- `/` - then we need __/__ again
- `$` - "the end"!

That means if you enter `http://127.0.0.1:8000/post/5/` into your browser, Django will understand that you look for a *view* called `post_detail` and transfer the information that `pk` equals `5`.
That means if you enter `http://127.0.0.1:8000/post/5/` into your browser, Django will understand that you are looking for a *view* called `post_detail` and transfer the information that `pk` equals `5` to that *view*.

`pk` is shortcut from `primary key`. This name is very often used in many Django projects. But you can name your variable as you like (remember: lowercase and `_` instead of whitespaces!). For example instead of `(?P<pk>[0-9]+)` we could have variable `post_id`, so this bit would look like: `(?P<post_id>[0-9]+)`.
`pk` is shortcut for `primary key`. This name is ften used in many Django projects. But you can name your variable as you like (remember: lowercase and `_` instead of whitespaces!). For example instead of `(?P<pk>[0-9]+)` we could have variable `post_id`, so this bit would look like: `(?P<post_id>[0-9]+)`.

Ok! Let's refresh the page:

Expand All @@ -77,21 +77,21 @@ Do you remember what the next step is? Of course: adding a view!

## post_detail view

This time our *view* is given an extra parameter `pk`. Our *view* needs to catch it, right? So we will define our function as `def post_detail(request, pk):`. Note that we need to use exactly the same name as the one we specified in urls (`pk`). Ommiting this variable is incorrect and will result in error!
This time our *view* is given an extra parameter `pk`. Our *view* needs to catch it, right? So we will define our function as `def post_detail(request, pk):`. Note that we need to use exactly the same name as the one we specified in urls (`pk`). Omitting this variable is incorrect and will result in an error!

Now, we want to get one and only one blog post. To do this we can use querysets like this:

Post.objects.get(pk=pk)

But this code has a problem. If there is no `Post` with given `primary key` (`pk`) we will have super ugly error!
But this code has a problem. If there is no `Post` with given `primary key` (`pk`) we will have a super ugly error!

![DoesNotExist error](images/does_not_exist2.png)

We don't want it! But, of course, Django comes with something that will handle that for us: `get_object_or_404`. In case there is no `Post` with given `pk` it will display much nicer page (called `Page Not Found 404` page).
We don't want that! But, of course, Django comes with something that will handle that for us: `get_object_or_404`. In case there is no `Post` with the given `pk` it will display much nicer page (called `Page Not Found 404` page).

![Page not found](images/404_2.png)

The good news is that you actually can create your own `Page not found` page and make it as pretty as you want. But it's not super important right now, so we will skip it.
The good news is that you can actually create your own `Page not found` page and make it as pretty as you want. But it's not super important right now, so we will skip it.

Ok, time to add a *view* to our `views.py` file!

Expand All @@ -115,7 +115,7 @@ It worked! But what happens when you click a link in blog post title?

![TemplateDoesNotExist error](images/template_does_not_exist2.png)

Oh no! Error once again! But we already know how to deal with it, right? We need to add a template!
Oh no! Another error! But we already know how to deal with it, right? We need to add a template!

We will create a file in `blog/templates/blog` called `post_detail.html`.

Expand All @@ -133,7 +133,7 @@ It will look like this:
<p>{{ post.text }}</p>
{% endblock %}

Once again we are extending `base.html`. In `content` block we want to display a post's published_date (if it exists), title and text. But we should discuss some important things, right?
Once again we are extending `base.html`. In the `content` block we want to display a post's published_date (if it exists), title and text. But we should discuss some important things, right?

`{% if ... %} ... {% endif %}` is a template tag we can use when we want to check something (remember `if ... else ..` from __Introduction to Python__ chapter?). In this scenario we want to check if a post's `published_date` is not empty.

Expand Down
13 changes: 6 additions & 7 deletions template_extending/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Template extending

Another nice thing Django has for you is __template extending__. What does it mean? It means that you can use the same parts of your HTML for different pages of your website.
Another nice thing Django has for you is __template extending__. What does this mean? It means that you can use the same parts of your HTML for different pages of your website.

This way you don't have to write it every time in every single html file you have, you don't repeat yourself and if you want to change something, you don't have to do it in every template, just once!
This way you don't have to repeat yourself in every file, when you want to use the same information/layout and, if you want to change something, you don't have to do it in every template, just once!

## Create base template

A base template is the most basic template that you use to extend on every page of your website.
A base template is the most basic template that you extend on every page of your website.

Create a `templates/mysite/base.html` file. You also need to create `templates` and `mysite` folders, but you probably have noticed this pattern already :)

Expand Down Expand Up @@ -69,10 +69,9 @@ We basically replaced everything between `{% for post in posts %}{% endfor %}` w
{% block content %}
{% endblock %}

What does it mean? You just created a `block`, which is a template tag that allows you to insert HTML in this place in templates that are extending `base.html`. We will show you how to do that in a moment.

Now save it, and open your `blog/templates/blog/post_list.html` again. Delete everything else than what's inside the body and then delete also `<div class="page-header"></div>`, so the file will look like this:
What does it mean? You just created a `block`, which is a template tag that allows you to insert HTML in this block in other templates that extend `base.html`. We will show you how to do this in a moment.

Now save it, and open your `blog/templates/blog/post_list.html` again. Delete everything else other than what's inside the body and then also delete `<div class="page-header"></div>`, so the file will look like this:

{% for post in posts %}
<div class="post">
Expand All @@ -82,7 +81,7 @@ Now save it, and open your `blog/templates/blog/post_list.html` again. Delete ev
</div>
{% endfor %}

And now add this line on the beginning of the file:
And now add this line to the beginning of the file:

{% extends 'mysite/base.html' %}

Expand Down

0 comments on commit 9fffb99

Please sign in to comment.