嘿伙计们我有一个应用程序的问题我试图在django中开发。我试图在管理界面中显示缩略图,但它显示的是网址。这是我的models.py
中的内容from sorl.thumbnail.main import DjangoThumbnail
from django.db import models
from django.contrib.auth.models import User
class Slide(models.Model):
#Image Fields
image = models.ImageField(
upload_to="/srv/www/htdocs/photos/gallery/images/",
height_field="image_height",
width_field="image_width"
)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
title = models.CharField(max_length=128)
order = models.PositiveIntegerField(default=0)
def slide_thumbnail(self, width=300, height=200):
if self.image:
thumb = DjangoThumbnail(self.image, (width, height))
return '{img src="%s" /}' % thumb.absolute_url
return '{img src="/media/img/admin/icon-no.gif" alt="False"}'
slide_thumbnail.allow_tags = True
def __unicode__(self):
return u'Slide: %s - %sx%s' % (self.title, self.image_height, self.image_width)
这就是我在admin.py中的内容
from django.contrib import admin
from models import *
from django.contrib import admin
class SlideAdmin(admin.ModelAdmin):
list_display = ('title', 'order', 'slide_thumbnail',)
admin.site.register(Slide, SlideAdmin)
我在suse 12.1上使用django 1.4并运行apache作为我的web服务器,并试图按照这里的教程进行操作:
http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/
但相反,我在“幻灯片缩略图”列中获取了一个网址或图片。 我得到的网址看起来像这样
{img src="/srv/www/htdocs/photos/gallery/images/702362_10200241914757028_37708631_n_jpg_300x200_q85.jpg" /}
有人可以告诉我出错了什么因为它上传图片但是没有显示它们
答案 0 :(得分:2)
您应该将img标记标记为安全,如下所示:
from django.utils.safestring import mark_safe
#... your code
return mark_safe('<img src="%s" />') % thumb.absolute_url
希望这会导致正确的方向。
答案 1 :(得分:0)
def image_tag(self):
return u'<img src="%s" />' % <URL to the image>
image_tag.short_description = 'Image'
image_tag.allow_tags = True
并在您的admin.py中添加:
readonly_fields = ('image_tag',)