I am nearing the final stages of a project and have run into a bit of a hiccup with Django.
It relates to the {% blocktrans %} tag.
How do I enable it to be fully functional in my app, currently if I wrap a piece of text in {% blocktrans %} I get a TemplateSyntaxError message
I have the following in my
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.i18n",
...
)
Any help would be appreciated.
For me was like this (windows + python 2.6 + django 1.2.1)
Will result an error(TemplateSyntaxError):
{% load i18n %}
{% blocktrans %} My name is {{ user.firstname }} {% endblocktrans %}
Will work:
{% load i18n %}
{% blocktrans with user.firstname as hmpf %} My name is {{ hmpf }} {% endblocktrans %}
Related
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.
What is the difference between these two lines of code in a Django template?
{% blocktrans %}Example{% endblocktrans %}
{% blocktranslate %}Example{% endblocktranslate %}
Likewise, what is the difference between these two lines of code in a Django template?
{% trans "Example" %}
{% translate "Example" %}
There is no difference. {% blocktrans %} is equivalent to {% blocktranslate %}, it's just an alias. Likewise, {% trans %} is equivalent to {% translate %}.
I know that I can register my model as a Django model and add an image there but I need to save a pages' tree and identify my pages by an image on a listing page. To make a long story short, I need to show images on the listing page in admin.
Create a template in templates/wagtailadmin/pages/listing/_page_title_explore.html
{% load i18n wagtailadmin_tags %}
{# The title field for a page in the page listing, when in 'explore' mode #}
<div class="title-wrapper">
{% if page.sites_rooted_here.exists %}
{% if perms.wagtailcore.add_site or perms.wagtailcore.change_site or perms.wagtailcore.delete_site %}
{% endif %}
{% endif %}
{% if page_perms.can_edit %}
{{ page.get_admin_display_title }}
{% else %}
{{ page.get_admin_display_title }}
{% endif %}
{% block pages_listing_title_extra %}
{% if page.youtube_video_id %}
<img src='https://i.ytimg.com/vi/{{page.youtube_video_id}}/mqdefault.jpg' width='150'/>
{% endif %}
{% endblock pages_listing_title_extra %}
{% include "wagtailadmin/pages/listing/_privacy_indicator.html" with page=page %}
{% include "wagtailadmin/pages/listing/_locked_indicator.html" with page=page %}
</div>
<ul class="actions">
{% page_listing_buttons page page_perms %}
</ul>
Pay attention on {% block pages_listing_title_extra %}, for example, you can add your variable from a model to this block.
Keep in mind that the admin template has been overwritten, and after upgrading the Wagtail core you may not see some new features.
If you just want to add image, you don't need to override entire page title explore template. You can use just pages_listing_title_extra block.
Create file templates/wagtailadmin/pages/listing/_page_title_explore.html with content:
{% extends "wagtailadmin/pages/listing/_page_title_explore.html" %}
{% load wagtailimages_tags %}
{% block pages_listing_title_extra %}
{% image page.image fill-100x200 %}
{% endblock pages_listing_title_extra %}
This should not break with new version of Wagtail (only if this template block would be removed/renamed).
in my server I have a lot of apps installed like, facebook_connect, userena, guardian and so on...
For example, I realized that if I customize the:
django-userena / userena / templates / userena / emails / activation_email_message.txt
{% load i18n %}{% autoescape off %}{% load url from future %}
{% if not without_usernames %}{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
{% endif %}
{% blocktrans with site.name as site %}Thank you for signing up at {{ site }}.{% endblocktrans %}
{% trans "To activate your account you should click on the link below:" %}
{{ protocol }}://{{ site.domain }}{% url 'userena_activate' activation_key %}
{% trans "Thanks for using our site!" %}
{% trans "Sincerely" %},
{{ site.name }}
{% endautoescape %}
For specified website, and I have more than 4 in the same server, I will make a complete mess in my django_site.
My question is:
How to customize the templates or models in some installed apps without completely change the original django_site?
Thanks in advance,
You cannot change the models, but you can override templates.
In the same directory as manage.py, you would have a directory called templates, there, you can create the following folder hierarchy, and put your custom template.
templates/userena/emails/activation_email_message.txt
Any idea why this doesn't work? It gives me an error at {% if tab.title==foc %}
{% for tab in menu %}
{% for foc in focus %}
<li>{{ tab.title }}</li>
{% if tab.title==foc %}
{% endif %}
{% endfor %}
{% endfor %}
Try it with spaces around ==
Alternatively, use the ifqual tag instead of if
The if statement was introduced on django 1.2 alpha and modified on django 1.2
The right way of using it:
{% if somevar == "x" %}
This appears if variable somevar equals the string "x"
{% endif %}
Check your django version at your django console with:
django.version
And if you using a lesser than 1.2 you should use the ifequal tag