Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions en/django_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,79 @@ __Django template tags__ allow us to transfer Python-like things into HTML, so y

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

To print a variable in Django templates, we use double curly brackets with the variable's name inside, like this:
To print a variable in Django templates, 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. Open it up in the code editor, and replace the existing `<article>` elements with `{{ posts }}`.

Your `blog/templates/blog/post_list.html` should now look like this:

{% filename %}blog/templates/blog/post_list.html{% endfilename %}
```html
{{ posts }}
<!DOCTYPE html>
<html>
<head>
<title>Django Girls blog</title>
</head>
<body>
<header>
<h1><a href="/">Django Girls Blog</a></h1>
</header>

{{ posts }}

</body>
</html>
```

Try this in your `blog/templates/blog/post_list.html` template. Open it up in the code editor, and replace the existing `<article>` elements with `{{ posts }}`. Save the file, and refresh the page to see the results:
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:

{% filename %}blog/templates/blog/post_list.html{% endfilename %}
```html
<QuerySet [<Post: My second post>, <Post: My first post>]>
```
`<QuerySet [<Post: My second post>, <Post: My first post>]>`

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:

{% filename %}blog/templates/blog/post_list.html{% endfilename %}
```html
```
{% for post in posts %}
{{ post }}
{% endfor %}
```

Try this in your template.
Try this in your template. Your `blog/templates/blog/post_list.html` should now look like this:

{% filename %}blog/templates/blog/post_list.html{% endfilename %}
```html
<!DOCTYPE html>
<html>
<head>
<title>Django Girls blog</title>
</head>
<body>
<header>
<h1><a href="/">Django Girls Blog</a></h1>
</header>

{% for post in posts %}
{{ post }}
{% endfor %}

</body>
</html>
```

Now refresh the web page:

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

It works! But we want the posts to be displayed like the static posts we created earlier in the __Introduction to HTML__ chapter. You can mix HTML and template tags. Our `body` will look like this:
It works! But we want the posts to be displayed like the static posts we created earlier in the __Introduction to HTML__ chapter. You can mix HTML and template tags. In the `blog/templates/blog/post_list.html` file, change this block of code from
```
{% for post in posts %}
{{ post }}
{% endfor %}
```

{% filename %}blog/templates/blog/post_list.html{% endfilename %}
```html
<header>
<h1><a href="/">Django Girls Blog</a></h1>
</header>
to

```
{% for post in posts %}
<article>
<time>published: {{ post.published_date }}</time>
Expand Down
12 changes: 12 additions & 0 deletions en/django_urls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ urlpatterns = [
]
```

Now your `blog/urls.py` file should look like this:

{% filename %}blog/urls.py{% endfilename %}
```python
from django.urls import path
from . import views

urlpatterns = [
path('', views.post_list, name='post_list'),
]
```

As you can see, we're now assigning a `view` called `post_list` to the root URL. This URL pattern will match an empty string and the Django URL resolver will ignore the domain name (i.e., http://127.0.0.1:8000/) that prefixes the full URL path. 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.

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.
Expand Down
18 changes: 15 additions & 3 deletions en/django_views/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ def post_list(request):
return render(request, 'blog/post_list.html', {})
```

As you can see, we created a function (`def`) called `post_list` that takes `request` and will `return` the value it gets from calling another function `render` that will render (put together) our template `blog/post_list.html`.
Now your `blog/views.py` file should look like this:

{% filename %}blog/views.py{% endfilename %}
```python
from django.shortcuts import render

def post_list(request):
return render(request, 'blog/post_list.html', {})
```

As you can see, we created a function called `post_list`. It takes `request` and will `render` (display) our template `blog/post_list.html`.

Save the file, go to http://127.0.0.1:8000/ and see what we've got.

Another error! Read what's going on now:
Another error! But don't worry, we will fix it. Let's first understand what the error message is telling us:

![Error](images/error.png)

This shows that the server is running again, at least, but it still doesn't look right, does it? Don't worry, it's just an error page, nothing to be scared of! Just like the error messages in the console, these are actually pretty useful. You can read that the *TemplateDoesNotExist*. Let's fix this bug and create a template in the next chapter!
This shows that the web server is running, at least, but it still doesn't look right, does it? Don't worry, it's just an error page, nothing to be scared of! Just like the error messages in the console, these are actually pretty useful. The *TemplateDoesNotExist* message indicates our template `blog/post_list.html` does not exist. Indeed, we have not created the template file yet.

Let's fix this by creating the template `blog/post_list.html` in the next chapter!

> Learn more about Django views by reading the official documentation: https://docs.djangoproject.com/en/5.1/topics/http/views/