From 67c3c820eb0693e394fd2dd24e3cea32ccb02283 Mon Sep 17 00:00:00 2001 From: zhlsk Date: Sun, 28 Sep 2025 13:46:11 +0200 Subject: [PATCH 1/3] Display what blog/views.py should look like. Also, make the description more understandable for beginners. --- en/django_views/README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/en/django_views/README.md b/en/django_views/README.md index 4c0d860c6d9..d5dcf6044ef 100644 --- a/en/django_views/README.md +++ b/en/django_views/README.md @@ -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/ From 066712d4e2d9da9b15832815bdfd73a3a8f48df8 Mon Sep 17 00:00:00 2001 From: zhlsk Date: Sun, 28 Sep 2025 14:01:30 +0200 Subject: [PATCH 2/3] Display what blog/urls.py file should look like. --- en/django_urls/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/en/django_urls/README.md b/en/django_urls/README.md index 961d3a0ea01..7ad57bb5aa2 100644 --- a/en/django_urls/README.md +++ b/en/django_urls/README.md @@ -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. From 92e48a8f7ed95ba437f69ad5cd6209e539752b9d Mon Sep 17 00:00:00 2001 From: zhlsk Date: Sun, 28 Sep 2025 14:25:17 +0200 Subject: [PATCH 3/3] Give more clear instrcutions on how to update the template file. --- en/django_templates/README.md | 68 ++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/en/django_templates/README.md b/en/django_templates/README.md index 6086ba883d5..b31b99ddfce 100644 --- a/en/django_templates/README.md +++ b/en/django_templates/README.md @@ -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 `
` 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 }} + + + + Django Girls blog + + +
+

Django Girls Blog

+
+ + {{ posts }} + + + ``` -Try this in your `blog/templates/blog/post_list.html` template. Open it up in the code editor, and replace the existing `
` 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 -, ]> -``` +`, ]>` 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 + + + + Django Girls blog + + +
+

Django Girls Blog

+
+ + {% for post in posts %} + {{ post }} + {% endfor %} + + + +``` + +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 -
-

Django Girls Blog

-
+to +``` {% for post in posts %}