13👍
In Django 1.9, you can use the libraries
option of DjangoTemplates
to include a tag library under a specified name. In the example below, the thumbnail library from sorl.thumbnail
is included under the name sorl_thumbnail
.
Note: the templatetag itself is not changed within the template… ie. remains
thumbnail
Usage:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "foo", "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries': {
'sorl_thumbnail': 'sorl.thumbnail.templatetags.thumbnail',
},
},
},
]
your_template.html
{% load sorl_thumbnail %}
{% thumbnail mymodel.image "640x480" crop="center" as im %}
<img src="{{ im.url }}" width="{{im.width}}" height="{{im.height}}"/>
{% endthumbnail %}
11👍
Sure, just write your own stub easy_thumbnail wrapper…
- Create a
thumbnailtags
package in one of your django apps… - …making sure it’s got an empty
__init__.py
-
In
thumbnailtags/easy_thumbnail.py
do something like:from django.template import Library from easy_thumbnails.templatetags import thumbnail register = Library() def easy_thumbnail(parser, token): return thumbnail(parser, token) register.tag(easy_thumbnail)
- Use
{% load easy_thumbnail %}
Note:
You might also be able to do ‘import thumbnail as easy_thumbnail, and skip the def easy_thumbnail
bit, tho I’ve not tried that.
6👍
This blog link shows how to handle this.
https://timmyomahony.com/blog/using-sorl-thumbnail-and-easy-thumbnails-same-template/
(previously
http://timmyomahony.com/blog/2012/10/22/using-sorl-thumbnail-and-easy-thumbnails-same-template/)
- Sending email from Django via hosted SMTP
- CORS django 'Access-Control-Allow-Origin'
- How to create multiple workers in Python-RQ?
- Django: How to get the root path of a site in template?
2👍
UPDATE 2015
I had to do the following modifications to Tom Christie’s answer in order to get this to work:
- create a
templatetags
package in one of you local apps. It is important to name ittemplatetags
. See django docs for template tags. - … make sure it has an
__init__.py
, empty or not. -
In
templatetags/easy_thumbnail.py
do this:from django.template import Library from easy_thumbnails.templatetags import thumbnail register = Library() def easy_thumbnail(parser, token): return thumbnail.thumbnail(parser, token) # the important bit register.tag(easy_thumbnail)
-
Use
{% load easy_thumbnail %}
or- load easy_thumbnail
with pyjade
- Django nginx Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- Is it possible to return an HttpResponse in django with text & a json object?
- Django – Change a ForeignKey relation to OneToOne