Python - creating a file to load

I want the PDF file in my project directory to be downloable, and not open in the browser when the user clicks the link.

I completed this question Creating a file to upload using Django

But I get an error message:

Exception Type: SyntaxError
Exception Value: can't assign to literal (views.py, line 119)
Exception Location: /usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module, line 35

I created a download link:

 <a href="/files/pdf/resume.pdf" target="_blank" class="btn btn-success btn-download" id="download" >Download PDF</a>

urls.py:

url(r'^files/pdf/(?P<filename>\{w{40})/$', 'github.views.pdf_download'),

views.py:

def pdf_download(request, filename):
    path = os.expanduser('~/files/pdf/')
    f = open(path+filename, "r")
    response = HttpResponse(FileWrapper(f), content_type='application/pdf')
    response = ['Content-Disposition'] = 'attachment; filename=resume.pdf'
    f.close()
    return response

Error line:

response = ['Content-Disposition'] = 'attachment; filename=resume.pdf'

How can i make it downloable?

Thank!

UPDATE

It works in Firefox, but not in Chrome v21.0.

+5
source share
2 answers

You have an extra =in this line, which makes the syntax invalid. It should be

response['Content-Disposition'] = 'attachment; filename=resume.pdf'

( , = : foo = bar = 'hello' , , . , .)

+5

, ,

def pdf_download(request, filename):
  path = os.expanduser('~/files/pdf/')
  wrapper = FileWrapper(file(filename,'rb'))
  response = HttpResponse(wrapper, content_type=mimetypes.guess_type(filename)[0])
  response['Content-Length'] = os.path.getsize(filename)
  response['Content-Disposition'] = "attachment; filename=" + filename
  return response
+3

All Articles