Python Forum
first django site-ms word/pdf files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: first django site-ms word/pdf files (/thread-5714.html)



first django site-ms word/pdf files - jon0852 - Oct-17-2017

I am developing my first website and would like a couple word documents and/or pdf's to be made available for download on the site. I'm assuming these are just considered static files, right? So, should those documents just go inside of the static files folder? Any guidance on how that may work would be appreciated!


RE: first django site-ms word/pdf files - homayoon_hashemi - Nov-19-2017

def download_course(request, id):
course = Courses.objects.get(pk = id)
path_to_file = get_path_to_course_download(course)

response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response

Where get_path_to_course_download returns the location of the download in the file system (ex: /path/to/where/handle_uploaded_files/saves/files/the_file.doc)