humanize "naturaltime" issue - django

I've got this strange problem where the humanize filters aren't working in one template, but work fine in another with exactly the same data. django.contrib.humanize has been added to my INSTALLED_APPS and the variable im calling naturalday on is a str which contains a ISO date without the time element e.g 2013-06-27
I can't figure out why it works in one template and not the other with exactly the same data. Instead the template instead just prints out the string contents.
Here's the template in question:
{% extends "base.html" %} {% load i18n %} {% load humanize %} {% block
content %}
<div class="large-7 columns">{% autoescape off %}
{{ product.img }}
{% endautoescape %}</div> {% if product.affiliate == 'no' %}<div class="large-12 columns main-posted">Posted: {{
product.dateAdded|naturalday }}</div>{% endif %}

Related

Jinja html code gets incorrectly formatted when saved in VS code

I am writting a Django project using VS Code. I find a strange Jinja formatting issue.
What I want is
{% extends "../base.html" %}
{% load static %}
{% block title %}
{% if category %} {{ category.name }} {% else %} Products {% endif %}
{% endblock %}
{% block content %} {% endblock content %}
But when I save the file or ctrl+s, it gets formatted like
{% extends "../base.html" %} {% load static %} {% block title %} {% if category
%} {{ category.name }} {% else %} Products {% endif %} {% endblock %} {% block
content %} {% endblock content %}
and the render gets error due to broken brackets.
I am using Jinja2 Snippet Kit and Prettier plugins. Do I need to make some changes in setting?
Could someone give me a help? Thank you.

request.user not available in Streamblocks subtemplate

In my main template I can call:
{% load static wagtailuserbar wagtailcore_tags %}
{% load navigation_tags %}
{% if request.user.is_authenticated %}
You're logged in
{% endif %}
but if I call this in my StreamBlock sub-template, it doesn't work.
{% load wagtailcore_tags wagtailimages_tags %}
{% if request.user.is_authenticated %}
<div class="container">
...
</div>
{% endif %}
Any ideas?
When outputting the StreamField onto the page, be sure to use the include_block tag rather than just outputting the value within a {{ ... }} tag. This ensures that any variables defined on the outer template, including request, are passed on to the sub-template. If you use {{ ... }}, the sub-template will still be rendered, but the only available variables are the ones supplied by the block itself.
{% include_block %} can be used on an individual block, or the stream as a whole:
{% load wagtailcore_tags %}
{% include_block page.body %}
or
{% for block in page.body %}
{% include_block block %}
{% endfor %}
You'll also need to do this within any sub-templates that render sub-sub-templates (for example, if you have a nested StreamBlock within the StreamField, the template for that StreamBlock needs to use include_block too).

Is there a way to pass a variable to an 'extended' template in Django?

I want to add some flexibility to my layout template, but I can't find any way to do so.
I'm looking for a way to extend my layout template with variable, i.e. to pass a variable up in the template tree, not down.
# views.py
def my_view_func(request):
return render(request, "child.html")
# child.html
{% extends 'layout.html' with show_sidebar=True sidebar_width_class="width_4" %}
<div>Templates stuff here</div>
# layout.html
{% if show_sidebar %}
<div class="{{ sidebar_width_class }}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
I have to maintain four templates with a difference in a few lines of code. For example, I have two templates that differ from each other by a sidebar width class. Am I doing something wrong?
I suspect that block is what you are looking for in the first place.
Form your block inside the base template like this:
{% block sidebar_wrapper %}
{% if sidebar %}
<div class="width{{sidebar_width}}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
{% endblock sidebar_wrapper%}
And on your child template:
{% extends 'layout.html' %}
{% block sidebar_wrapper %}
{% with sidebar=True sidebar_width=4 %}
{{ block.super }}
{% endwith%}
{% endblock sidebar_wrapper%}
What you need is an include template tag. You can include a template in another template and render that with specific context.
{% include 'layout.html' with sidebar=True sidebar_width=4 %}
Check docs here: https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#include
You can achieve this with some technique. I'll show the code then explain below.
# layout.html
{% block content %}
{% if show_sidebar %}
<div class="{{ sidebar_width_class }}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
{% endblock %}
# child.html
{% extends 'layout.html' %}
{% block content %}
{% with show_sidebar=True sidebar_width_class="width_4" %}
{{ block.super }}
{% endwith %}
{% endblock %}
In layout.html, wrap everything inside {% block content %}
In child.html, {{ block.super }} is like python's super(), which renders everything in the parent template's block. So if you wrap it inside {% with %} tag, all variables that you declare there will be available inside the parent template as well.

How to extend a base.html using two different files?

I'm writing an application in which user can choose one of several tools for data analysis and open it in a panel on main page. Is it possible to use django "extends" and have each tool defined in different file?
The minimal example of what im strugling with would be like this:
base.html
<div>
{% block left_panel %}
left block
{% endblock content%}
</div>
<div>
{% block right_panel %}
right block
{% endblock %}
</div>
and sample left_panel and right_panel tools:
left1.html
{% extends "base.html" %}
{% block left_panel %}
<p>TEST left 1</p>
{% endblock %}
right1.html
{% extends "base.html" %}
{% block right_panel %}
<p>TEST right 1</p>
{% endblock %}
Is there a way to render the base.html with both blocks overwriten?
I believe that the best way to implement your requirement is to create a new template that extends base.html and includes left1.html and right1.html. Something like this:
{% extends "base.html" %}
{% block left_panel %}
{% include "left1.html" %}
{% endblock content%}
{% block right_panel %}
{% include "right1.html" %}
{% endblock %}
Update based on OP's comment: Actually you just need one configurable template, not 100. Let's say that based on the tools the user selects, your view passes the left_tool and right_tool context variables to your template. Now, you can easily do something like this:
{% block left_panel %}
{% if left_tool == "tool1" %}
{% include "left1.html" %}
{% elif left_tool == "tool2" %}}
{% include "left2.html" %}
etc ...
{% else %}
{% include "left10.html" %}
{% endif %}
{% endblock content%}
You'll do the same with the right panel. Of course the above is a little naive and definitely not DRY -- instead you could for instance generate the name of the template to be included in the view and pass it directly to the template, or use a custom node etc.

Django translation in template: trans works "trans as" not

I'm new to django and try the translation feature in views. I came across following problem:
I tried to translate a text into a variable, but this is always empty. However if I just output it works fine.
{% extends "myownapp/base.html" %}
{% load i18n %}
{% trans "Test" as test %} <--- here it is defined
{% block title %}Title - {% trans "Test" %}{% endblock %} <--- does work
{% block content %}
<h1>{{ test }}</h1> <--- does not work
{% endblock %}
Note: I haven't created the language files yet - could this be the problem? Thanks
You need to put {% trans "Test" as test %} into the template block in which you are using the variable:
{% block content %}
{% trans "Test" as test %}
<h1>{{ test }}</h1>
{% endblock %}