文章目录
  1. 1. 主要两步走(1、修改存储到storage下;2、修改展示页面地址)
    1. 1.1. 1、修改存储到storage下
    2. 1.2. 2、修改展示页面地址

主要两步走(1、修改存储到storage下;2、修改展示页面地址)

下面详细说说

1、修改存储到storage下

  • 新增storage存储方法(sae论坛中找到,代码共享于此,保存到zinnia/models_bases/file.py )
#file.py
from django.db import models
from django.db.models.fields.files import ImageFieldFile,FieldFile,FileField,ImageFile
import sae.storage
class SAEFieldFile(FieldFile):
    def getUploadTo(self):
        return self.upload_to
    def save(self, name, content, save=True):
        name = self.field.generate_filename(self.instance, name)
        #for SAE
        s = sae.storage.Client()
        ob = sae.storage.Object(content._get_file().read())
        url =s.put('media', name, ob)
        self.name = name
        setattr(self.instance, self.field.name, self.name)
        # Update the filesize cache
        self._size = content.size
        self._committed = True
        # Save the object because it has changed, unless save is False
        #if save:
        #    self.instance.save()
class SAEImageFieldFile(ImageFile, SAEFieldFile):
    def delete(self, save=True):
        # Clear the image dimensions cache
        if hasattr(self, '_dimensions_cache'):
            del self._dimensions_cache
        super(ImageFieldFile, self).delete(save)
class ZGImageFieldFile(SAEImageFieldFile):
    def save(self, name, content, save=True):
        super(SAEImageFieldFile, self).save(name, content, save=True)
class ZGImageField(models.ImageField):
    attr_class = ZGImageFieldFile
    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
        super(ZGImageField, self).__init__(verbose_name, name, **kwargs)
  • 修改entry的model (zinnia/models_bases/entry.py )
from file import ZGImageField
class ImageEntry(models.Model):
    """
    Abstract model class to add an image to the entries.
    """
    #print 'ImageEntry'
    #image = models.ImageField(
    #    _('image'), blank=True, upload_to=UPLOAD_TO,
    #    help_text=_('Used for illustration.'))
    #image = ZGImageField(upload=UPLOAD_TO)
    image = ZGImageField(
        _('image'), blank=True, upload_to=UPLOAD_TO,
        help_text=_('Used for illustration.'))
    class Meta:
        abstract = True
  • 测试存储,会保存到storage下的image域 uploads/下

2、修改展示页面地址

修改模板展示,暂时硬编码修改img处(我这里用的是zinnia_bootstrap视图,所以修改zinnia_bootstrap/templates/zinnia/_entry_detail_base.html)

<div class="entry-image">
      <p>
        // if continue_reading //
        <a href="" title="" rel="bookmark">
          // endif //
          <img src="http://afeiblog-media.stor.sinaapp.com/" alt="" class="img-thumbnail img-responsive" />
          // if continue_reading //
        </a>
        // endif //
      </p>
    </div>

最后测试成功,上传截图,请观赏

文章目录
  1. 1. 主要两步走(1、修改存储到storage下;2、修改展示页面地址)
    1. 1.1. 1、修改存储到storage下
    2. 1.2. 2、修改展示页面地址