Django admin and showing thumbnail images(Django 管理员并显示缩略图)
问题描述
我正在尝试在 Django 管理中显示缩略图,但我只能看到图像的路径,而不是渲染的图像.我不知道我做错了什么.
I'm trying to show thumbnail images in Django admin, but I can only see the path to the images, but not the rendered images. I don't know what I'm doing wrong.
服务器媒体网址:
from django.conf import settings
(r'^public/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
功能模型:
def image_img(self):
if self.image:
return u'<img src="%s" />' % self.image.url_125x125
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
admin.py:
class ImagesAdmin(admin.ModelAdmin):
list_display= ('image_img','product',)
结果:
<img src="http://127.0.0.1:8000/public/product_images/6a00d8341c630a53ef0120a556b3b4970c.125x125.jpg" />
推荐答案
这在 photologue(参见 models.py
,稍作修改以删除不相关的内容):
This is in the source for photologue (see models.py
, slightly adapted to remove irrelevant stuff):
def admin_thumbnail(self):
return u'<img src="%s" />' % (self.image.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True
list_display
位看起来也一样,我知道这很有效.对我来说唯一可疑的是您的缩进-在 models.py
代码末尾以 image_img
开头的两行应该与 def image_img(self):
,像这样:
The list_display
bit looks identical too, and I know that works. The only thing that looks suspect to me is your indentation - the two lines beginning image_img
at the end of your models.py
code should be level with def image_img(self):
, like this:
def image_img(self):
if self.image:
return u'<img src="%s" />' % self.image.url_125x125
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
这篇关于Django 管理员并显示缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 管理员并显示缩略图
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01