1đź‘Ť
Template tags in the Django template engine are simple functions that accept values and parameters. They can all be accessed directly in the source code.
This is output from IPython:
In [173]: from django.template.defaultfilters import date
In [174]: date??
Type: function
String Form:<function date at 0xa2935f0>
File: /usr/local/python/thor/lib/python2.7/site-packages/django/template/defaultfilters.py
Definition: date(value, arg=None)
Source:
@register.filter(expects_localtime=True, is_safe=False)
def date(value, arg=None):
"""Formats a date according to the given format."""
if not value:
return u''
if arg is None:
arg = settings.DATE_FORMAT
try:
return formats.date_format(value, arg)
except AttributeError:
try:
return format(value, arg)
except AttributeError:
return ''
Most of the Django-specific magic resides in the decorator (register.filter
) and in the way that the language resolves calls when you type {% load <template library> %}
in your template. Look for the definitions in <module>.templatetags
modules. Built-ins are located in django.template.defaulttags
and django.template.defaultfilters
.
If Jinja2 has a way to define new template filters (which it probably does), then you could simply wrap handmade template filters to actual Django functions.
So basically, just create Jinja2 filters that point to the actual Django function definitions.
Edit:
If you do not have access to the actual Django functions, just copy-paste the source code and remove or adapt Django-specific stuff.
The escapejs
filter is actually a call to this function in django.utils.html
:
_base_js_escapes = (
('\\', r'\u005C'),
('\'', r'\u0027'),
('"', r'\u0022'),
('>', r'\u003E'),
('<', r'\u003C'),
('&', r'\u0026'),
('=', r'\u003D'),
('-', r'\u002D'),
(';', r'\u003B'),
(u'\u2028', r'\u2028'),
(u'\u2029', r'\u2029')
)
# Escape every ASCII character with a value less than 32.
_js_escapes = (_base_js_escapes +
tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))
def escapejs(value):
"""Hex encodes characters for use in JavaScript strings."""
for bad, good in _js_escapes:
value = mark_safe(force_unicode(value).replace(bad, good))
return value
1đź‘Ť
I alluded to this in my comment, but I’ll be more specific here since I have more space. Django is an end-to-end web application framework, which happens to include its own template language, that for lack of a better term is just called the “Django template language”. All the template tags and filters in the docs are specific to that language.
If you choose to use Jinja2, you choose to use that template language’s structures to the exclusion of Django’s. (Obviously the model and view stuff is a separate layer, but in principle, those could be swapped too–it’s all loosely coupled.) So the Django docs for templating are no good to you in that case. If you want to format a float in Jinja2, it looks like you need to use the format
filter, according to the Jinja docs.
Without more info, I really can’t say what’s causing your ImportError, but floatformat
is not going to work in a Jinja2 template, so maybe that has something to do with it.
- [Answered ]-Django-pipeline wipes out my entries in Django Database Cache
- [Answered ]-Django uploading a file from a form fails to validate
- [Answered ]-How to pass multiple app's models in one views? [django]
- [Answered ]-Django display image on RadioSelect or Checkboxes
- [Answered ]-Confusion between celery and celeryd