简体   繁体   中英

Django Admin Sorl-Thumbnail Link

I am having trouble adding an html A tag around the img tag. When I add the A Tag with the variable, it stops showing the image in the Django Admin, but without the variable in the A tag it shows the image. I can't seem to figure this out, and possibility it may not be supported by sorl-thumbnail?

*UPDATE Trying to pass two variables now with different thumbnail sizes, but it's not doing anything. I'm new working with definitions in Django Admin.

def screenshot_img(self, obj):
    if obj.screenshot:

            s = get_thumbnail(obj.screenshot,"x200",crop='center', quality=99)
            b = get_thumbnail(obj.screeshot,"x500",crop='center', quality=99)                

            return u'<a href="/im_troubleticket/media/%b"><img src="/im_troubleticket/media/%s"></a>' % (s, b)
    else:
            return u'No image'


screenshot_img.short_description = 'Picture'
screenshot_img.allow_tags = True

You are using the variable twice in the string, so you need to supply it twice:

return u'<a href="/im_troubleticket/media/%s"><img src="/im_troubleticket/media/%s"></a>' % (s, s)

EDIT: You have two new bugs in your code: First, you misspelled obj.screenshot. Second, the placeholders in your string indicate the type of data, not the name of the variable. See Python String Formatting . If you use descriptive variable names you are less likely to confuse the s in %s with the name of the variable.

thumb_small = get_thumbnail(obj.screenshot,"x200",crop='center', quality=99)
thumb_large = get_thumbnail(obj.screenshot,"x500",crop='center', quality=99)                

return u'<a href="/im_troubleticket/media/%s"><img src="/im_troubleticket/media/%s"></a>' % (thumb_small, thumb_large)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM