{% trans "string" as my_translated_string %} is not rendering content in template - django

Django 1.4 doc says that you can get translated strings into "vars" to be used in different places or to be used as arguments in template tags or filters using the following syntax:
{% trans "String" as my_translated_string %}
<h1>{{ my_translated_string }}</h1>
https://docs.djangoproject.com/en/1.4/topics/i18n/translation/#trans-template-tag
I'm doing it in that way, however the defined var is never rendering the content. Below my template code:
{% extends "default_layout.html" %}
{% load i18n %}
{% trans "My page title" as title %}
{% block meta_title %}{{ title }}{% endblock %}
{% block content %}
<h1>{{ title }}</h1>
{% endblock %}
Of course "title" is being rendered empty in both cases.
Am I missing something ?
Thank you.

As okm said you need to define the variable within the block in which you want to use it, and the scope of that variable is also within the block:
{% extends "default_layout.html" %}
{% load i18n %}
{% block meta_title %}
{% trans "My page title" as title %}
{{ title }}
{% endblock %}
{% block content %}
{% trans "My page title" as title %}
<h1>{{ title }}</h1>
{% endblock %}

Related

How to complement the block of parent template?

I have first.html:
{% block title %}
hi there
{% endblock %}
{% block content %}
blah-blah
{% endblock %}
{% block footer %}
bye
{% endblock %}
and second.html.
How can I incude all first.html page to second.html and complement content block like this:
hi there
blah-blah
this text is from second.html
bye
I've tryed
{% extends "first.html" %}
this text is from second.html
(just nothing added) and
{% extends "first.html" %}
{% block content %}
this text is from second.html
{% endblock %}
(block content is overrided).
You can make use of {{ block.super }} [Django-doc] here when you want to render the original content as well. For example:
{% extends 'first.html' %}
{% block content %}
{{ block.super }}
this text is from second.html
{% endblock %}

{% block something %} always being displayed despite being False in an if-statement

I have two types of blog articles. Regular blog articles, and translation articles. They both have different html markup. I have a boolean variable translation_bool in my models to check if it is a translation article or not. If it is I want it to display my {% block translation %} and if not {% block translation %}. It worked with plain html code and not using html tags. But I had so much reusable code that it got troublesome to manage.
So my question is: why is this happening despite it being inside of an if statement.
Article template:
{% extends "base_generic.html" %}
{% load static %}
{% block js %}...{% endblock %}
{% if blogpost.translation_bool == True %}
{% block translation %}....{% endblock %}
{% else %}
{% block content %}...{% endblock %}
{% endif %}
{% block sidebar %}....{% endblock %}
In Base Generic Template:
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-8">
{% block content %}{% endblock %}
{% block translation %}{% endblock %}
</div>
<div class="col-md-3">
{% block social_media %}...{% endblock %}
{% block sidebar %}...{% endblock %}
</div>
</div>
</body>
This is because blocks not defined in child template will render value from parent template. So in your case you should perform validation inside parent template. Or if it's impossible override blocks in child with empty content:
{% block translation %}
{% if blogpost.translation_bool == True %}
{{ block.super }}
{% else %}
{% endif %}
{% endblock %}
{% block content %}
{% if blogpost.translation_bool == False %}
{{ block.super }}
{% else %}
{% endif %}
{% endblock %}
Note {{ block.super }} will render content from parent template.

Djangocms template not showing up

I have a basic DjangoCMS up and running.
base.html contains:
{% block content %}{% endblock content %}
I also have feature.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
<div>
{% placeholder "feature2" %}
</div>
<div class="jumbotron"">
{% placeholder "feature" %}
</div>
<div>
{% placeholder "content" %}
</div>
{% endblock content %}
I added the "feature2" placeholder in the above, and it correctly displays for editing on the site.
I then added a new line to base.html:
{% block base_logo %}{% endblock base_logo %}
and created a new file, base_logo.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_logo %}
<div>
{% placeholder logo %}
</div>
{% endblock base_logo %}
I expected this to also appear on the site for editing, but it doesnt. I have added the base_logo.html to the CMS_TEMPLATES in settings.py and TEMPLATE_DIR is also pointing correctly.
What else do I need to do for Djangocms to pick up my new template?
Take a look at template inheritance.
You're trying to use two {% extends %} tags, which won't work. You should use the {% include %} tag for base_logo, because it seems you'd want to include this in many templates. This question provides more info.

django TemplateSyntaxError Invalid block tag: 'trans'

After running the runserver command I get the following error:
TemplateSyntaxError at /questions/ Invalid block tag: 'trans'
Does anybody know that what's the reason?
This is my template syntax:
{% extends "two_column_body.html" %}
{#
this template is split into several
blocks that are included here
the blocks are within directory templates/main_page
relative to the skin directory
there is no html markup in this file
#}
<!-- questions.html -->
{% block forejs %}
{% include "main_page/custom_head_javascript.html" %}
{% endblock %}
{% block title %}{% spaceless %}{% trans %}Questions{% endtrans %}{% endspaceless %}{% endblock %}
{% block content %}
{% include "main_page/tab_bar.html" %}
{% include "main_page/headline.html" %}
{# ==== BEGIN: main_page/content.html === #}
<div id="question-list">
{% include "main_page/questions_loop.html" %}
</div>
{# ==== END: main_page/content.html === #}
{% include "main_page/paginator.html" %}
{% endblock %}
{% block sidebar %}
{% include "main_page/sidebar.html" %}
{% endblock %}
{% block endjs %}
<script type="text/javascript">
{# cant cache this #}
askbot['settings']['showSortByRelevance'] = {{ show_sort_by_relevance|as_js_bool }};
askbot['messages']['questionSingular'] = '{{ settings.WORDS_QUESTION_SINGULAR|escapejs }}';
askbot['messages']['answerSingular'] = '{{ settings.WORDS_ANSWER_SINGULAR|escapejs }}';
askbot['messages']['acceptOwnAnswer'] = '{{ settings.WORDS_ACCEPT_OR_UNACCEPT_OWN_ANSWER|escapejs }}';
askbot['messages']['followQuestions'] = '{{ settings.WORDS_FOLLOW_QUESTIONS|escapejs }}';
</script>
{% include "main_page/javascript.html" %}
{% include "main_page/custom_javascript.html" %}
{% endblock %}
<!-- end questions.html -->
{% trans %}Questions{% endtrans %} is not the correct format.
{% load i18n %} should be at the top of your template, or any extended template using translations.
You can use {% trans "Questions." %}
If you're going to use blocks, they need to be in the format below:
{% blocktrans %}{{ value2translate }}{% endblocktrans %}
More info here.
Probably you should use {% blocktrans %}Questions{% endblocktrans %} and you forget to put {% load i18n %} toward the top of your template.
this is because you have not loaded i18n in this template{% load i18n %} you must add this in each of your template.
You must place at the beginning of your extended template code: {% load i18n %} , so you can use the trans Tags:
{% extends 'home/base.html' %}
{% block title %}INICIO{% endblock %}
{% load i18n %}
{% block opcionesMenu %}
<!-- =====START====== -->
{% trans "Inicio" %}
{% trans "Proyectos" %}
{% trans "DiseƱo Web" %}
{% trans "Marketing" %}
{% trans "Conocenos" %}
{% trans "Contacto" %}
<!-- =====END ====== -->
{% endblock %}

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 %}