1

I'm creating a website where i can upload catalogues on a downloads page and users can download them, i can achieve this for a single file by using the following code in the view, but i need a simpler way i can do this on the admin or on a template.

def brochure(request):
 fs = FileSystemStorage()
 filename = 'kastom_profile.pdf'
 if fs.exists(filename):
    with fs.open(filename) as pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        response['Content-Disposition'] = 'inline;filename="kastom_profile.pdf"'
        return response
  else:
    return HttpResponseNotFound('The requested pdf was not found in our server.')
Dennis Gathagu
  • 105
  • 1
  • 14
  • Are you running your server with apache? You could just serve the files with apache or any other webserver. Or do you want to stick to django for the whole project? If so, why? – Tim Woocker Mar 28 '18 at 22:20
  • I'm using nginx as my web server, I wanted to use django to do all the work since i didn't know there is another way to get around my problem. – Dennis Gathagu Mar 28 '18 at 22:39
  • Have a look at this question. It could help you set up nginx to serve files outside of django. Django can then just redirect to the requested files. This is the way django is intended to work so I suggest you have a look at this. – Tim Woocker Mar 28 '18 at 22:43
  • i'd be very grateful if you guided me through the whole process of setting up nginx, you haven't provided a link to the question though – Dennis Gathagu Mar 28 '18 at 22:48
  • oops, sorry. Here is the link: https://stackoverflow.com/questions/2451739/django-serving-static-files-through-nginx you can find many tutorials on how to setup nginx with django. Just search for it on google :) – Tim Woocker Mar 28 '18 at 22:52

0 Answers0