Django Template tag, generating template block tag
Posted
by Issy
on Stack Overflow
See other posts from Stack Overflow
or by Issy
Published on 2010-04-18T20:51:20Z
Indexed on
2010/04/18
20:53 UTC
Read the original article
Hit count: 404
Hi Guys,
Currently a bit stuck, wondering if anyone can assist. I am using django-adminfiles. Which is a near little application. I want to use it to insert images into posts/articles/pages for a site i am building.
How django-adminfiles works is it inserts a placeholder i.e <<< ImageFile >>> and this gets rendered using a django template. It also has the feature of inserting custom options i.e (Insert Medium Image) , i figured i would used this to automatically resize images and include it in the post (similar to how WP does it).
Django-adminfiles makes use of sorl.thumbnail app to generate thumbnails.
So i have tried testing generating thumbnails:
The current template that is used to render the inserted image is:
{% spaceless %}
<img src="{{ upload.upload.url }}" width="{{ upload.width }}" height="{{ upload.height }}" class="{{ options.class }}" class="{{ options.size }}" alt="{% if options.alt %}{{ options.alt }}{% else %}{{ upload.title }}{% endif %}" />
{% endspaceless %}
I tried modifying this to:
{% load thumbnail %}
{% spaceless %}
<img src="{% thumbnail upload.upload.url 200x50 %}" width="{{ upload.width }}" height="{{ upload.height }}" class="{{ options.class }}" class="{{ options.size }}" alt="{% if options.alt %}{{ options.alt }}{% else %}{{ upload.title }}{% endif %}" />
{% endspaceless %}
I get the error:
Exception Value:
Caught an exception while rendering: Source file: '/media/uploads/DSC_0014.jpg' does not exist.
I figured the thumbnail needs the absolute path so tried putting that in the template, and that works.
i.e this works:
{% thumbnail '/Users/me/media/uploads/DSC_0014.jpg' 200x50 %}
So basically i need to generate the absolute path to the file give the relative path (to web root). You could do this by passing the MEDIA_ROOT setting to the template, but the reason i want to do a template tag is to programmatically set the image size.
© Stack Overflow or respective owner