views:

325

answers:

1

Hi Guys,

Currently a bit stuck, wondering if anyone can assist. I am using django-adminfiles. Which is a neat 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.

Update Tried this as per answer below:

templatetag:

from django import template
from proj.settings import MEDIA_ROOT

register = template.Library()

@register.filter(name='path')
def path(value):
    value = MEDIA_ROOT + '/' + value 
    return value

Template:

{% load thumbnail %}
{% load images %}
{% spaceless %}
<img src="{{ upload.upload.url|path }}" width="{{ upload.width }}" height="{{ upload.height }}" class="{{ options.class }}" class="{{ options.size }}" alt="{% if options.alt %}{{ options.alt }}{% else %}{{ upload.title }}{% endif %}" />
{% endspaceless %}
A: 

sorl needs a file system location since it's a service side process. Assuming your file system address mirror your web addresses, you should be able to create a custom filter to convert file locations.

UPDATE

I was suggesting something like this:

{% thumbnail upload.upload.url|path 200x50 %}
Zach
How would that work? as Thumbnail is a tag as well, can you have two tags i.e {% thumbnail filter %}
izzy
Hi Just tested this, and it works. However, the problem is you are inserting an img src= with an absolute path on your server. Does not seem correct. Will update question with what i have.
izzy
hi, thanks but as noted it inserts the absolute url (on the server) in the img src
izzy