gpt4 book ai didi

python - 图像未显示在 Django 应用程序上

转载 作者:行者123 更新时间:2023-12-01 05:58:10 26 4
gpt4 key购买 nike

我是一名 Django 初学者,正在尝试完成 lightbird.net 上的本教程来构建照片共享应用程序。我在将照片显示在开发服务器上时遇到问题。虽然我可以在管理界面上上传和保存图像,但照片也不会显示在那里。相反,我看到的只是一个 jpeg 图标。当我在开发服务器上查看页面时,出现相同的 jpeg 图标而不是照片。

我昨天花了一段时间才安装 PIL 和 jpeg 库,虽然我的应用程序现在运行没有错误,但我怀疑图像不显示的原因与这两个安装之一有关。我认为这可能是浏览器问题,但我无法从 firefox、google chrome 或 safari 查看图像。

预先感谢您的帮助。这个网站太棒了!

代码的 html 是:

{% extends "pbase.html" %}

{% block content %}
<div class="main">
<!-- Albums -->
<ul>
{% for album in albums.object_list %}
<div class="title">{{ album.title }} ({{ album.image_set.count}} images)</div>
<ul>
{% for img in album.images %}
<a href="{{ media_url }}{{ img.image.name }}"><img border="0" alt=""
src="{{ media_url }}{{ img.thumbnail2.name }}" /></a>
{% endfor %}
</ul>
{% endfor %}
</ul>

<!--Next/Prev page links-->
{% if albums.object_list and albums.paginator.num_pages > 1 %}
<div class="pagination">
<span class="step-links">
{% if albums.has_previous %}
<a href= "?page={{ albums.previous_page_number }}">previous &lt;&lt;</a>
{% endif %}

<span class="current">
&nbsp;Page {{ albums.number }} of {{ albums.paginator.num_pages }}
</span>
{% if albums.has_next %}
<a href="?page={{ albums.next_page_number }}"> &gt;&gt; next</a>
{% endif %}
</span>
</div>
{% endif %}
</div>
{% endblock %}

View 是:

def main(request):
albums = Album.objects.all()
if not request.user.is_authenticated():
albums = albums.filter(public=True)

paginator = Paginator(albums, 10)
try: page = int(request.GET.get("page", "1"))
except ValueError: page = 1

try:
albums = paginator.page(page)
except(InvalidPage, EmptyPage):
albums = paginator.page(paginator.num_pages)

for album in albums.object_list:
album.images = album.image_set.all()[:4]

return render_to_response("list.html", {'albums':albums, 'user':request.user, 'media_url':MEDIA_URL})

我的模型是:

class Album(models.Model):
title = models.CharField(max_length=60)
public = models.BooleanField(default=False)
def __unicode__(self):
return self.title

def images(self):
lst = [x.image.name for x in self.image_set.all()]
lst = ["<a href='/media/%s'>%s</a>" % (x, x.split("/")[-1])for x in lst]
return join(lst, ",")
images.allow_tags = True


class Tag(models.Model):
tag = models.CharField(max_length=60)
def __unicode__(self):
return self.tag


class Image(models.Model):
title = models.CharField(max_length=60, blank=True)
image = models.FileField(upload_to="images/")
tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)
created = models.DateTimeField(auto_now_add=True)
rating = models.IntegerField(default=50)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(User, null=True, blank=True)

thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True)

def save(self, *args, **kwargs):
"""save image dimensions."""
super(Image, self).save(*args, **kwargs)
im = PImage.open(os.path.join(MEDIA_ROOT, self.image.name))
self.width, self.height = im.size

# large thumb
fn, ext = os.path.splitext(self.image.name)
im.thumbnail((128,128), PImage.ANTIALIAS)
thumb_fn = fn + "-thumb2" + ext
tf2 = NamedTemporaryFile()
im.save(tf2.name, "JPEG")
self.thumbnail2.save(thumb_fn, File(open(tf2.name)), save=False)
tf2.close()

#small thumb
im.thumbnail((40,40), PImage.ANTIALIAS)
thumb_fn = fn + "-thumb" + ext
tf = NamedTemporaryFile()
im.save(tf.name, "JPEG")
self.thumbnail2.save(thumb_fn, File(open(tf.name)), save=False)
tf.close()

super(Image, self).save(*args, **kwargs)

def size(self):
return "%s x %s" % (self.width, self.height)

def tags_(self):
lst = [x[1] for x in self.tags.values_list()]
return str(join(lst, ', '))

def __unicode__(self):
return self.image.name

def albums_(self):
lst = [x[1] for x in self.albums.values_list()]
return str(join(lst, ', '))

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.allow_tags = True

最佳答案

关于python - 图像未显示在 Django 应用程序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11570491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com