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
- [Answered ]-Django Model.objects.all() and Model.objects.get(pk=?) return two different results
- [Answered ]-Django Insert Data file location
- [Answered ]-Django ModelAdmin Fieldset within fieldset
- [Answered ]-Order of select_related in chain
Source:stackexchange.com