Home > OS >  HTML <a> download Attribute works when opened in html but not when I run it on main.py
HTML <a> download Attribute works when opened in html but not when I run it on main.py

Time:11-19

On a flask site, link to download pdf is provided on the page.

When i run the site by main.py i get the file doesn't exist error as given under,

but it works fine when i run only index.html file

the error

in main.py

class ThePage(MethodView):

    def get(self):
        form = InfoForm()
        return render_template("index.html")

in index.html

    <a href="../1668671114.892154.pdf" download>
            <div >
                <span >&#9989;</span> <span style="font-size: 20px">PDF ready, click.</span>
            </div>
    </a>

Problem may be related to render_template part where i should add some arguments, though i couldn't figure out how to.

CodePudding user response:

I think you are using a relative path to the templates directory. The server cannot resolve this URL. Use the static folder to deliver static files.

  • Move the pdf file to a folder called static within your application's root directory.
  • Inside the anchor tag use url_for('static', filename='1668671114.892154.pdf')
<a href="{{ url_for('static', filename='1668671114.892154.pdf') }}" download>
    <div >
        <span >&#9989;</span> <span style="font-size: 20px">PDF ready, click.</span>
    </div>
</a>
  • Related