Форум сайта python.su
('^data/(.+)$', files),
Что должна возвращать функция files чтоб при переходе по ссылке скачивать фаил?
Офлайн
HttpResponse должна вернуть, как и обычно.
Только нужно правильно указать mimetype и добавить заголовок Content-Disposition. Например,
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=my.pdf'
## ...
return response
Офлайн
А если я не знаю точно какой тип файла?
И где должен хранится это т самый my.pdf
Офлайн
def show_attachment(request, hash):
attachment = get_object_or_404(Attachment, hash=hash)
file_data = file(attachment.get_absolute_path(), 'rb').read()
response = HttpResponse(file_data, mimetype=attachment.content_type)
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(attachment.name)
return response
Офлайн