django - {% blocktrans %} {{rendered_variable}} {% endblocktrans %} - python

django trans is not working for me in this case:
{% blocktrans %} {{sign}} {% endblocktrans %}
the {{sign}} are coming from views.py and are Sunsigns like:
'Capricorn'
'Aquarius'
'Pisces'
'Aries'
'Taurus'
'Gemini'
'Cancer'
'Leo'
'Virgo'
'Libra'
'Scorpio'
'Sagittarius'
I added into .po file all their translations and did compilemessages but it is just not translating it. what am I doing wrong?

blocktrans is for translating the text around a variable, but it won't translate the variable itself.
This answer can be helpful for you. More info in docs.

I just did the translation in models.py like this:
_('Capricorn')
_('Aquarius')
_('Pisces')
_('Aries')
_('Taurus')
_('Gemini')
_('Cancer')
_('Leo')
_('Virgo')
_('Libra')
_('Scorpio')
_('Sagittarius')
and in template just
{{sign}}
and it is working.

Related

Django Allauth: How to customize confirmation email subject?

I was able to customize the confirmation email HTML template by adding this file into the templates folder:
templates/account/email/email_confirmation_signup_message.html
Now I'm trying to customize the subject of the email by adding the text I want inside this file:
templates/account/email/email_confirmation_signup_subject.txt
But it doesn't seem to do anything, I still get the default subject all the time.
Does anyone know what I'm doing wrong?
Many thanks!
You have to add this two files...
first:
account/email/email_confirmation_signup_message.html
{% include "account/email/email_confirmation_message.html" %}
second:
account/email/email_confirmation_message.html
The template that you want.
and finally delete the two .txt file:
account/email/email_confirmation_signup_message.txt
account/email/email_confirmation_message.txt
pd: if you copy the folder template/account from your virtual enviroment you have to delete the files over there as well.
Do in this way...
install django-mail-templated
base template file code...
{{ TAG_START_SUBJECT }}
{% autoescape off %}
{% block subject %}
{% endblock %}
{% endautoescape %}
{{ TAG_END_SUBJECT }}
main template file code...
{% block subject %}
Hello User..
{% endblock %}
Change file name to this:-
templates/account/email/email_confirmation_subject.txt
and inside write
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Please Confirm Your E-mail Address or do whatever..{% endblocktrans %}
{% endautoescape %}
Maybe the problem comes with your urls.py file
You need to indicate here the file you use to customize the subject of your email.
PasswordResetView.as_view(template_name='email_confirmation_signup_message.html', subject_template_name='email_confirmation_signup_subject.txt')
For me the default behavior was to have the template in the the project folder and not in the particular apps directory api_project/templates/account/email/email_confirmation_subject.txt!
For anybody that might be here just because [example.com] still appears at the subject even when they customized successfully the templates/account/email/email_confirmation_subject.txt a variable that you need to configure and it's not quit obvious it's ACCOUNT_EMAIL_SUBJECT_PREFIX = '[example.com]'

Build HTML files using Django Template System or Static Site Generator?

I am trying to set up a proper workflow for a personal website.
I am using the Cactus static site generator which makes use of the Django template system.
I know what I want to do, but I am not sure how to do it as the tutorials for Cactus are limited.
This is what my directory structure looks like:
/mysite/pages/
/mysite/templates/
/mysite/mycontent/
/mysite/plugins/
My template, /mysite/pages/menu.html, looks like this:
<p>Welcome to the page!</p>
{% block body %}
{% endblock %}
And one of my page articles, /mysite/pages/testpage.html, looks like this
{% extends "menu.html" %}
{%block body %}
<p> Test Content </p>
{% endblock %}
But what I am trying to do is set this up so that whatever I want to write for Test Content can be written somewhere else and the copied in to the folder. Something like this:
{% extends "menu.html" %}
{%block body %}
{%include "../mysite/mycontent/TestContent.html}
{% endblock %}
Is this something that Django templates needs to manage? Like I said, I am using Cactus which uses Django templates but I have looked around and am not sure what the standard way of doing this is, even though it seems to work with MVC/MVT philosophy.
There is also an option to use Django plugins with Cactus.
Thanks!
I figured out what I was trying to do. I just had to include the html I wanted using the template language.
{% extends "menu.html" %}
{%block body %}
{% include "./file.html" %}
{% endblock %}

Django TemplateSyntaxError in with template tag

I'm trying to use this app in my project.
https://github.com/streema/django-favit
I already can use the fav-unfav part of this app. I also want to list favourites of user for every user. In read me part it says use this and it will be listed but I have an error with
{% with user_favorites <user> "baslik.Entry" as favorite_list %}
{% for fav_obj in favorite_list %}
{{ fav_obj }}
{% endfor %}
{% endwith %}
Error:
TemplateSyntaxError at /
u'with' expected at least one variable assignment
This is the template tag part for user_favorites:
#register.assignment_tag
def user_favorites(user, app_model=None):
"""
Usage:
Get all user favorited objects:
{% with user_favorites <user> as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{% endfor %}
{% endwith %}
or, just favorites from one model:
{% with user_favorites <user> "app_label.model" as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{%
{% endwith %}
"""
return Favorite.objects.for_user(user, app_model)
How can I get rid of this error? Thanks.
It's a reasonably common convention in documentation that anything in angle brackets is a placeholder to be replaced by the actual value. In this case, <user> is supposed to be replaced by the object containing the actual user.
{% with user_favorites request.user ...
I must say, though, that the documentation still doesn't make any sense. You can't use an assignment tag in a with statement like that - even after correcting the user issue, this still won't work. The confusing thing is that the same syntax is repeated throughout the documentation, but it simply doesn't work.
I think this is simply a bug with the documentation, and suspect that if you simply remove the word "with" this will work.
To use custom template tag in django, it is needed to explicitly load it in template.
Add this line at the beginnig of your template (but after {% extends ... %}, if you have such):
{% load favit_tags %}
Looks like this step is missed from django-favit README.

Django filter field on the context processor

I want to filter a result of a field in django in an html file.
Something like this
{{ model.field where id = 2 }}
I've been looking for in django docs but i only could find a way to do it on the views.py.
I also so something like javascript when u write a "|" simbol after the request but i still couldnt archieve it
You can use the {% if %} template tag. So:
{% if model.field == 2 %}
# do something
{% endif %}
Here is the official documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#operator
Edit:
If model.field has a value of 2 then it just needs to be the above.
Edit 2:
Without seeing your code, it is hard to tell, but here is how to filter for Users based on Gender in a template:
{% for user in users %}
{% if user.gender == "male" %}
# do something
user.username
{% endif %}
{% endfor %}

Is there an equivalent of the "blocktrans" in tag in jinja2?

I am using jinja2 with my django application, I am working on porting some existing django templates over to jinja2. For the most part I am not having any issues; however, I have a statement like so in one of my django templates:
{% blocktrans %}
<p>Some stuff here</p>
{% endblocktrans %}
This causes jinja2 to become very unhappy. I have looked far and wide to see if there is an equivalent of the "blocktrans" tag for jinja2. All I can find is that enabling gettext let's you use:
{{ trans("Some String") }}
This is useful for short strings like in the title tag, but not for blocks. What am I missing, any help would be appreciated!
Use {% trans %} and its complement {% endtrans %} once the i18n extension is enabled.

Categories

Resources