moritzs / jinja2-django-tags Goto Github PK
View Code? Open in Web Editor NEWjinja2 extensions that add django tags
License: MIT License
jinja2 extensions that add django tags
License: MIT License
Hi.
When I updated jinja2 verion 3, templates using blocktrans started failing:
from django.template import engines
env = engines.all()[0].env
env.from_string("{% blocktrans %}test.key{% endblocktrans %}")
raises an error
Error
Traceback (most recent call last):
File "/opt/project/tests/test_helpers.py", line 203, in test_without_nulls
template = self.env.from_string("{% blocktrans %}test.key{% endblocktrans %}")
File "/app/pip/lib/python3.10/site-packages/jinja2/environment.py", line 1092, in from_string
return cls.from_code(self, self.compile(source), gs, None)
File "/app/pip/lib/python3.10/site-packages/jinja2/environment.py", line 757, in compile
self.handle_exception(source=source_hint)
File "/app/pip/lib/python3.10/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: unexpected end of template, expected 'end of statement block'.
Hi.
I'm not understanding, how date region dependent localization works.
{{ date|localize }}
? I always get isoformat now, my FORMAT_MODULE_PATH is ignored,Hi.
I may have misunderstood something, but I can't make it working.
In russian language nouns are pluralized into 4 forms, not 2. Example:
{% blocktrans count counter=stats['users_count'] %}Уникальный посетитель{% plural %}Уникальные посетители{% endblocktrans %}
And .po file:
#: apps/jinja2/emails/app_stats_email.jinja2:348
msgid "Уникальный посетитель"
msgid_plural "Уникальные посетители"
msgstr[0] "Уникальный посетитель"
msgstr[1] "Уникальных посетителя"
msgstr[2] "Уникальных посетителей"
If I understood correctly, django's template pluralization should output one of msgstr[0] - msgstr[2] depending on the counter. But it always outputs msgid_plural.
You should ping coffin to use those. Special thanks for the fact that you've implemented the pgettext with blocktrans context.
I'm now replacing my jinja2 trans
tags with this because jinja2 doesn't support context...
First of all, this project is awesome! I don't understand why it doesn't have more traction. It's certainly more convenient than https://github.com/niwinz/django-jinja
Anyhow, for more Django tag compatibility it would help a lot to have a load tag that does absolutely nothing,
The code itself can easily be borrowed from coffin: https://github.com/cdleary/coffin/blob/05ab3fc79fe0c1b2aa4103f67520e109d660b999/coffin/template/defaulttags.py#L9-L24
Hi!
I'm a django-jinja developer and maintainer.
First of all, great work. I thinking that it make sense join forces and integrate that into django-jinja directly. If you are interested we can talk how we can colaborate.
Making this extensions easy available for all versions of django, not only django 1.8.
That do you thing about?
In Django 4.0+ an ImportError exception is raised on from django.utils.encoding import force_text
as it is renamed to forse_str
Hi! I am switching from Django templates to Jinja and trying to write my own Extensions, by studying your code in extensions.py. Thank you for providing this guide.
I have a tag that takes 1 required positional arg and 1 optional kwarg:
{% form_field 'age' label='how old are you?' %}.
I know that in Jinja this should be a macro like {% form_field('age', label='how old are you?') %}
, but for compatibility I need to preserve the old Django-style syntax.
I am wondering about the simplest way to implement this? Because this template tag is just like a python function with 2 args (no special syntax or custom keywords like with
, as
, etc).
Or do I need to parse the tokens one by one, check if each is TOKEN_STRING, TOKEN_NAME, etc.? Although I read your code, I am not sure how to deal with the case where the args are context variables, e.g.:
context = {'field_name': 'age', 'label': 'How old are you?'}
{% form_field field_name label=label %}
.
Any advice? Thank you so much.
Template:
{% blocktrans count candy_num=4 %}
{{ candy_num }} candy
{% plural %}
{{ candy_num }} candies
{% endblocktrans %}
Result:
'candy_num' is undefined
In extensions.py
, things seem to get wrong at line 266:
if context is None:
return ugettext(force_text(singular)) % trans_vars
else:
return pgettext(force_text(context), force_text(singular)) % trans_vars
else:
if context is None:
return ungettext(
force_text(singular), force_text(plural), trans_vars[count_var] # errorneous
) % trans_vars
else:
return npgettext(
force_text(context), force_text(singular), force_text(plural),
trans_vars[count_var]
) % trans_vars
Local vars:
Variable | Value |
---|---|
context | None |
count_var | 'candy_num' |
plural | Markup('\n %(candy_num)s candies\n') |
self | <jdj_tags.extensions.DjangoCompat object at 0x…> |
singular | Markup('\n %(candy_num)s candy\n') |
trans_vars | {'candy_num': Undefined} |
Hi.
I use gunicorn server served by supervisor (in venv). When I reboot supervisor code is updated.
But when I cleanly install django-jinja on the server via pip and release project version with django-jinja included libs, I get an error:
("expected token 'name', got 'string'",)
in jinja2 templates on statements like:
{% trans 'panel.emails.password_reset.link_valid_interval' %}
After some time, the error is gone (some sort of caching?) and never occures again. The error can at the same time appear on one template and don't appear on another one.
I've tried deleting pycache directory in all project directories, but it didn't resolve the issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.