views:

307

answers:

2

I'm using appengine patch with django 1.1 (came with appengine patch). I have a page with multiple columns and multiple files. In the admin I want to be able to upload an image and write text in a column like so:

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

(where get_absolute_url shows me /pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/)

My trouble is getting the image to be rendered on the page, the above shows 'didnt work' instead of rendering the image. I can go to the detail pages of page, column and file though. Have I not done the models correctly, does the app.yaml need something, should i not be passing a template for the file's generic url?

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

My 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()])

my 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
+1  A: 

"/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/" should return an image. As far as I can tell from your urls.py right now you return html back (Would have to see the template to be certain)

Here's a view that would return a JPEG image back with the proper mimetype

def get_image(request, object_id):
    file = get_object_or_404(File, pk=object_id)
    return HttpResponse(file.data, mimetype='image/jpeg')

Then change urls.py to

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

Steven, thanks for thinking with me, the / seems to be necessary though.
Nathan, this solved the problem.

I made the following changes:

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

and in models.py:

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

Thanks!

Glad I could help.
Nathan