From a918ec9dbfeb3c1d20e3f6c424274e13909d121e Mon Sep 17 00:00:00 2001 From: Arnold Krille Date: Sat, 26 Jul 2014 18:24:22 +0200 Subject: [PATCH 1/2] use safe and linebreaks filters for the text --- django_templates/README.md | 4 ++-- template_extending/README.md | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/django_templates/README.md b/django_templates/README.md index 670c35ecaac..8966e5f5032 100644 --- a/django_templates/README.md +++ b/django_templates/README.md @@ -44,7 +44,7 @@ It works! But we want them to be displayed in a way we created earlier in the __

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text }}

+

{{ post.text|safe|linebreaks }}

{% endfor %} @@ -52,7 +52,7 @@ Everything you put between `{% for %}` and `{% endfor %}` will be repeated for e ![Figure 13.3](images/step3.png) -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. +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. Also the `|safe|linebreaks` is piping the posts text through filters so it preserves html and converts line-breaks to paragraphs. 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. diff --git a/template_extending/README.md b/template_extending/README.md index 44a868f3a17..33db70253ab 100644 --- a/template_extending/README.md +++ b/template_extending/README.md @@ -39,7 +39,7 @@ Then open it up and copy everything from `post_list.html` to `base.html` file, l

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text }}

+

{{ post.text|safe|linebreaks }}

{% endfor %} @@ -77,7 +77,7 @@ Now save it, and open your `blog/templates/blog/post_list.html` again. Delete ev

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text }}

+

{{ post.text|safe|linebreaks }}

{% endfor %} @@ -94,7 +94,7 @@ It means that we're now extending `base.html` template in `post_list.html`. Only

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text }}

+

{{ post.text|safe|linebreaks }}

{% endfor %} {% endblock content %} @@ -102,5 +102,3 @@ It means that we're now extending `base.html` template in `post_list.html`. Only That's it! Check if your website is still working properly :) > If you have an error `TemplateDoesNotExists` that says that there is no `mysite/base.html` file and you have `runserver` running in the console, try to stop it (by pressing Ctrl+c - Control and C buttons together) and restart it with `python manage.py runserver`. - - From 96c48f68e023cbcfc9033b7956fc992059c824ce Mon Sep 17 00:00:00 2001 From: Arnold Krille Date: Sat, 26 Jul 2014 23:50:50 +0200 Subject: [PATCH 2/2] Adding the safe filter is too much Just add the linebreaks filter so the new users don't get distressed by their carefully prepared multi-paragraph blogposts on how great Django is becoming garbled into an unreadable one-paragraph text blob... --- django_templates/README.md | 4 ++-- template_extending/README.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/django_templates/README.md b/django_templates/README.md index 8966e5f5032..fd413a85bc0 100644 --- a/django_templates/README.md +++ b/django_templates/README.md @@ -44,7 +44,7 @@ It works! But we want them to be displayed in a way we created earlier in the __

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text|safe|linebreaks }}

+

{{ post.text|linebreaks }}

{% endfor %} @@ -52,7 +52,7 @@ Everything you put between `{% for %}` and `{% endfor %}` will be repeated for e ![Figure 13.3](images/step3.png) -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. Also the `|safe|linebreaks` is piping the posts text through filters so it preserves html and converts line-breaks to paragraphs. +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. Also the `|linebreaks` is piping the posts text through a filter to convert line-breaks into paragraphs. 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. diff --git a/template_extending/README.md b/template_extending/README.md index 33db70253ab..b5b02daa25c 100644 --- a/template_extending/README.md +++ b/template_extending/README.md @@ -39,7 +39,7 @@ Then open it up and copy everything from `post_list.html` to `base.html` file, l

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text|safe|linebreaks }}

+

{{ post.text|linebreaks }}

{% endfor %} @@ -77,7 +77,7 @@ Now save it, and open your `blog/templates/blog/post_list.html` again. Delete ev

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text|safe|linebreaks }}

+

{{ post.text|linebreaks }}

{% endfor %} @@ -94,7 +94,7 @@ It means that we're now extending `base.html` template in `post_list.html`. Only

published: {{ post.published_date }}

{{ post.title }}

-

{{ post.text|safe|linebreaks }}

+

{{ post.text|linebreaks }}

{% endfor %} {% endblock content %}