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

Issue

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 class="btn-block2">
                <span class="hazir">&#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.

Solution

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 class="btn-block2">
        <span class="hazir">&#9989;</span> <span style="font-size: 20px">PDF ready, click.</span>
    </div>
</a>

Answered By – Detlef

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published