我应该如何使用django在appengine上存储和检索图像?

时间:2009-11-03 13:38:07

标签: django image google-app-engine django-admin

我正在使用带有django 1.1的appengine补丁(带有appengine补丁)。 我有一个包含多列和多个文件的页面。 在管理员中,我希望能够上传图像并在列中写入文本,如下所示:

This is the sory of bugs bunny ...
<img src="/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/" alt="didnt work" />
some more text about bugs bunny ...

(其中get_absolute_url显示我/ pages / file / agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM /)

我的麻烦是要在页面上渲染图像,上面显示“没有工作”而不是渲染图像。 我可以去页面,列和文件的详细页面。 我没有正确完成模型,app.yaml是否需要一些东西,我不应该为文件的通用URL传递模板吗?

我的urls.py:

urlpatterns += patterns('',
    url(r'^index/$', direct_to_template, 
        {'template': 'base.html'}, name="main-view"),

    url(r'^pages/page/(?P<object_id>.+)/$', object_detail, 
        {'queryset': Page.all(), 
         'template_name': 'pages_page_detail.html'}, name="pages_page_detail_view"),

    url(r'^pages/column/(?P<object_id>.+)/$', object_detail, 
        {'queryset': Column.all(), 
         'template_name': 'pages_column_detail.html'}, name="pages_column_detail_view"),    

    url(r'^pages/file/(?P<object_id>.+)/$', object_detail, 
        {'queryset': File.all(), 
         'template_name': 'pages_file_detail.html'}, name="pages_file_detail_view"),    

    url(r'^pages/$', object_list, 
        {'queryset': Page.all(), 'paginate_by': 3, 'template_name': 'pages_page_index.html'}, name="page_index_view"), 
)

我的models.py:

class Page(db.Model):
    """Page model."""

    title           = db.StringProperty(_('title'), required=True)
    author          = db.ReferenceProperty(User)
    status          = db.StringProperty(_('status'), choices=[_('draft'), 
                                                              _('public'),
                                                              _('hidden'),
                                                     ])                                      
    created         = db.DateTimeProperty(_('created'))
    modified        = db.DateTimeProperty(_('modified'))
    non_indexable   = db.BooleanProperty(_('hide from index'))

    def __unicode__(self):
        return '%s' % self.title

    @permalink    
    def get_absolute_url(self):
        return ('pages_page_detail_view', [self.key()])

class File(db.Model):
    """File model."""
    title           = db.StringProperty(_('title'))
    data            = db.BlobProperty()
    added           = db.DateTimeProperty(_('added'))
    page            = db.ReferenceProperty(Page)
    #content_type    = FakeModelProperty(ContentType, required=True)

    def __unicode__(self):
        return '%s' % self.get_absolute_url()

    @permalink
    def get_absolute_url(self):
        return ('pages_file_detail_view', [self.key()])

class Column(db.Model):
    """Column model."""
    title           = db.StringProperty(_('title'))
    data            = db.TextProperty(_('data'))
    page            = db.ReferenceProperty(Page)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('pages_column_detail_view', [self.key()])

我的app.yaml:

application: aep-sample
version: 1
runtime: python
api_version: 1

default_expiration: '3650d'

handlers:
- url: /remote_api
  script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
  secure: optional
  login: admin

- url: /media
  static_dir: _generated_media
  secure: optional

- url: /.*
  script: common/appenginepatch/main.py
  secure: optional

2 个答案:

答案 0 :(得分:1)

“/ pages / file / agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM /”应返回图像。据我现在你可以告诉你的urls.py你现在返回html(必须看模板才能确定)

这是一个可以使用正确的mimetype

返回JPEG图像的视图
def get_image(request, object_id):
    file = get_object_or_404(File, pk=object_id)
    return HttpResponse(file.data, mimetype='image/jpeg')

然后将urls.py更改为

url(r'^pages/file/(?P<object_id>.+)/$', get_image)

答案 1 :(得分:1)

史蒂文,谢谢你和我一起思考,/似乎是必要的 内森,这解决了这个问题。

我做了以下更改:

urls.py:

url(r'^pages/file/(?P<object_id>.+)/$', 'pages.views.download_file', 
    name="pages_file_download_view"),

views.py:

def download_file(request, object_id):
    file = get_object_or_404(File, object_id)
    return HttpResponse(file.data, 
        content_type=guess_type(file.title)[0] or 'application/octet-stream')

和models.py:

@permalink
def get_absolute_url(self):
    return ('pages_file_download_view', [self.key()])

谢谢!