[Answered ]-How should I store and retrieve images with Django on App Engine?

1πŸ‘

β€œ/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πŸ‘

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!

πŸ‘€Dan1ell

Leave a comment