gpt4 book ai didi

python - 管理员中的 stdimage 图像路径无效

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:12 25 4
gpt4 key购买 nike

我对 Django 有点陌生,我在我的模型中使用了 stdimage 模块。当我通过管理员上传图像并保存时,在打开模型对象时,我发现该字段包含三样东西

  1. 当前图片:提供指向已上传图片的链接,例如 uploaded_images/2012/11/28/map_7.jpeg
  2. 改变:这是改变图像的规定
  3. 删除:带有用于删除上传图像的复选框。当我添加图像并保存对象时,我在/admin/oxer/site/41/收到错误 IOError 并且当我单击图像链接时出现 404 错误,图像 url 显示为 itech.com/admin/oxer/site/49/uploaded_images/2012/11/04/image_1.jpeg 而不是 itech.com/static/uploaded_images/2012/11/04/image_1.jpeg(注意我删除了'admin/oxer/site/49'并替换为“静态”)。

我猜ioerror是错误的url带来的。我怎样才能让它有正确的 url?

这是我的模型:

class Site(CommonMixin, ImageMixin):

__metaclass__ = TransMeta

name = models.CharField(max_length=255, verbose_name=_("Name"))
description = models.TextField(verbose_name=_("Description"))
has_airport = models.BooleanField(default=False)
rating = models.IntegerField()
order = models.IntegerField()
do_not_miss = models.TextField(verbose_name=_("Do not miss"))
recommended_stay = models.TextField(verbose_name=_("Recommended stay"))
address = models.CharField(max_length=255, verbose_name=_("Address"))
state = models.CharField(max_length=100, verbose_name=_("State"))
price = models.TextField(verbose_name=_("Price"))
good_to_know = models.TextField(verbose_name=_("Good to know"))
link = models.URLField(blank=True, null=True)
region = models.ManyToManyField('Region', blank=True, null=True)
interests = models.ManyToManyField('Interest', blank=True, null=True)
map = StdImageField(
upload_to='uploaded_images/%Y/%m/%d',
max_length=255,
height_field='height',
width_field='width',
size=(453, 294, True),
thumbnail_size=(195, 150, True),
blank=True,
null=True
)
location_lat = models.DecimalField(
u'Location (latitude)', max_digits=10, decimal_places=7, default=0,
help_text=u"You can use http://www.getlatlon.com to get a location's coordinates"
)
location_lon = models.DecimalField(
u'Location (longitude)', max_digits=10, decimal_places=7, default=0,
help_text=u"You can use http://www.getlatlon.com to get a location's coordinates"
)

最佳答案

一个ImageField的URL是这样的

upload_to='uploaded_images/%Y/%m/%d',

此路径与您的 MEDIA_URL 相关。假设您正在访问 http://site/admin/auth/user。如果在这个页面中你有这种类型的链接

<a href="uploaded_images/2012/11/28/file.ext">Anchor</a>

链接将指向

http://site/admin/auth/user/uploaded_images/2012/11/28/file.ext

这是你得到的,但显然是错误的。

长话短说,答案是您必须在存储路径前添加 MEDIA_URL这可能是

MEDIA_URL = '/media/' (the slash at the beginning points to the domain root)

MEDIA_URL = 'http://domain/media/' (might be redundant)

关于python - 管理员中的 stdimage 图像路径无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13608887/

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