4

I am using python flask. I want to provide a url in template which varies in filename.

Folder structure:

project_folder/download/pdf <-images in this folder

url_for('download/pdf/filenameXYZ.pdf') didnt worked.

davidism
  • 121,510
  • 29
  • 395
  • 339
beebek
  • 1,893
  • 5
  • 29
  • 33

2 Answers2

4

If you want to return static content you should tell Flask that you want static content :)

As you can see in this example: http://flask.pocoo.org/docs/patterns/jquery/?highlight=static

You need something like this:

url_for('static', filename='download/pdf/filenameXYZ.pdf') 
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • Download works... Can't I put the download folder as in level of static folder ??? – beebek Aug 20 '13 at 14:40
  • Yes, as long as your downloads folder is a subdirectory of your static folder it should work ilke that. Alternatively, you can also add the redirect in your webserver somewhere. – Wolph Aug 20 '13 at 17:34
0

url_for takes a flask function that has a route associated with it and any parameters you want to pass to that view. What url_for will do is return the URL path for that function.

If you want to display a PDF then you need to render a template which contains a HTML or tag. There's actually a bunch of different ways to handle this

An example that you could use in a template is

<embed src="/download/pdf/the.pdf" width="500" height="375">

the main point is that url_for doesn't take a file path.

Community
  • 1
  • 1
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103