我正在尝试配置我的管理员,以便显示图片的缩略图。我点击缩略图(没有图片)的所有内容都会将我链接到我的项目主页,而不是查看图像。
点击缩略图后的URL输出:
http://127.0.0.1:8000/admin/portfolio/photo/%7B%20MEDIA_URL%20%7D%s
目前的情况如下:
不确定为什么要显示图像= /
Here is my models.py for app:
from django.db import models
from django.contrib import admin
from PIL import Image
from Boothie.settings import MEDIA_ROOT
import os.path
from django.utils.html import format_html
class Album(models.Model):
title = models.CharField(max_length=50)
# thumbnail = models.ImageField()
def __str__(self):
return self.title
def images(self):
lst = [x.photo.title for x in self.photo_set.all()]
return lst
class AlbumAdmin(admin.ModelAdmin):
# Set search_fields to enable a search box on the admin change list page.
# This should be set to a list of field names that will be searched whenever
# somebody submits a search query in that text box.
search_fields = ["title"]
# Set list_display to control which fields are displayed on the change list page of the admin.
list_display = ["title"]
class Photo(models.Model):
title = models.CharField(max_length=50, blank=True)
album = models.ForeignKey(Album)
photo = models.FileField(upload_to="album")
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
upload = models.DateTimeField(auto_now_add=True)
# thumbnail = models.ImageField()
def save(self, *args, **kwargs):
# Save image dimensions
# Save image
super(Photo, self).save(*args, **kwargs)
# get image
pic = Image.open(os.path.join(MEDIA_ROOT, self.photo))
# set Photo width and height
self.width, self.height = pic.size
# save object
super(Photo, self).save(*args, **kwargs)
def __str__(self):
return self.title
def albums_(self):
# https://docs.djangoproject.com/en/1.7/ref/models/querysets/#values-list
lst = [x[1] for x in self.album.value_list()]
return ', '.join(lst)
def size(self):
# Photo size
return "%s x %s" % (self.width, self.height)
def thumbnail(self):
# return """<a href="/media/%s"><img border="0" alt="" src="/media/%s" height="40" /></a>""" % (self.image.name, self.image.name)
thumbnail_html = format_html("<a href=\"{{ MEDIA_URL }}%s\"><img border=\"0\" alt=\"\" src=\"{{ MEDIA_URL }}%s\" height=\"40\" /></a>", self.photo.name, self.photo.name)
return thumbnail_html
# thumbnail.allow_tags = True
class PhotoAdmin(admin.ModelAdmin):
search_fields = ["title", "photo"]
list_display = ["photo", "thumbnail", "title", "album", "size"]
list_filter = ["album"]
settings.py媒体设置:
MEDIA_ROOT = os.path.join(BASE_DIR, "../media_root/Boothie/pics")
MEDIA_URL = '/media/'
admin.py:
from django.contrib import admin
from portfolio.models import Album, AlbumAdmin, Photo, PhotoAdmin
admin.site.register(Album, AlbumAdmin)
admin.site.register(Photo, PhotoAdmin)
答案 0 :(得分:1)
我看到你已经在你的设置中定义了MEDIA_ROOT and MEDIA_URL
,然后只需在你的url.py文件中添加以下代码,这应该可以解决问题。
from django.conf import settings
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
答案 1 :(得分:1)
{{ MEDIA_URL }}
是模板标签,在模板中使用它。
在python代码中执行:
from django.conf import settings
settings.MEDIA_URL