admin中的stdimage图像路径无效

时间:2012-11-28 15:44:07

标签: python django

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

  1. 当前图片:提供已上传图片的链接,例如uploaded_images / 2012/11/28 / map_7.jpeg
  2. 更改:更改图像的条款
  3. 删除:带复选框,用于删除上传的图像。 当我添加图像并保存对象时,我在/ admin / oxer / site / 41 /得到错误IOError,当我点击图片链接时,我得到404错误,图片网址显示为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'并用'static'代替。
  4. 我想ioerror是由错误的网址引起的。我怎么能让它有正确的网址?

    这是我的模特:

    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"
        )
    

1 个答案:

答案 0 :(得分:0)

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)