Login

Sorl thumbs for the lazy

Author:
renyi
Posted:
May 22, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

Sorl thumbs for the lazy.

Usage: {% thumb p.image "noimage.png" %}

tags.py goes into your templatetags directory. includes/thumb.html goes into your templates directory.

Fork it here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''
Sorl thumbs for the lazy.

Usage: {% thumb p.image "noimage.png" %}

[Fork it here.](https://gist.github.com/985439)
'''

# tags.py
from django import template
register = template.Library()

@register.inclusion_tag("includes/thumb.html")
def thumb(img_obj, no_image="noimage.png", dim="100x100", no_image_dim="100px", ori="600"):
	return { 'img_obj': img_obj,
	         'no_image':no_image,
	         'dim': dim,
	         'no_image_dim':no_image_dim,
	         'ori':ori, }

# includes/thumb.html
{% load thumbnail %}

{% thumbnail img_obj dim crop="80% top" as im %}
	{% thumbnail img_obj ori as ori %}
		<a class="product_image" href="{{ ori.url }}" title="{{ img_obj|title }}">
			<img src="{{ im.url }}" style="width:{{ im.width }};height:{{ im.height }};">
		</a>
	{% endthumbnail %}
{% empty %}
	<img src="{{ no_image }}" style="width:{{ no_image_dim }};height:{{ no_image_dim }};">
{% endthumbnail %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.