13

I'm working on and project that uses two separate modular Django apps. However, one app requires easy-thumbnails and the other requires sorl-thumbnails. Unfortunately, the two thumbnail libraries make use of the template tag syntax {% load thumbnail %}, so they clash and break when a template using them tries to render.

Are there any approaches to solve this type of clash? (For example, a template option does to the effect of {% load thumbnail as easy_thumbnail %}). Am I going to have to fork one of the apps and replace one of the thumbnail libraries with another? If so, which should I choose to go with?

Thank you for considering my question, Joe

Joe J
  • 9,985
  • 16
  • 68
  • 100
  • 1
    you should give https://github.com/codysoyland/django-smart-load-tag a try :) – Paulo Nov 18 '11 at 00:13
  • That's a really neat module that seems like it will solve exactly this problem. I think I saw a django ticket to add similar functionality to django trunk. I hope it makes it in. Nice that this module exists for current and past instances of django. – Joe J Nov 18 '11 at 17:29

4 Answers4

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 %}
rjmoggach
  • 1,458
  • 15
  • 27
mnieber
  • 992
  • 9
  • 23
11

Sure, just write your own stub easy_thumbnail wrapper...

  1. Create a thumbnailtags package in one of your django apps...
  2. ...making sure it's got an empty __init__.py
  3. 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)
    
  4. 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.

Tom Christie
  • 33,394
  • 7
  • 101
  • 86
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/)

rjmoggach
  • 1,458
  • 15
  • 27
Salma Hamed
  • 2,000
  • 4
  • 26
  • 45
  • corrected response to show new url - https://timmyomahony.com/blog/using-sorl-thumbnail-and-easy-thumbnails-same-template/ – rjmoggach Feb 11 '16 at 18:09
2

UPDATE 2015

I had to do the following modifications to Tom Christie's answer in order to get this to work:

  1. create a templatetags package in one of you local apps. It is important to name it templatetags. See django docs for template tags.
  2. ... make sure it has an __init__.py, empty or not.
  3. 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)
    
  4. Use {% load easy_thumbnail %} or - load easy_thumbnail with pyjade

creimers
  • 4,975
  • 4
  • 33
  • 55