I tried to read file print.html
that located in path: templates/print.html
.
So, I used url_for()
to refer to it as below:
{{ url_for('templates', filename='print.html') }}
However, I got an error as below Traceback:
BuildError: ('templates', {'filename': 'print.html'}, None)
What's wrong with this?
If I used {{ url_for('static', filename='print.html') }}
, it just working find. However, file that I tried to read is not in static
folder, it was in templates/print.html
instead.
How can I use url_for()
to read my file print.html
in templates
folder? Thanks.
I'll start by saying-- if you're trying to url_for
a template, it's likely you're misunderstanding a concept. But, assuming you're know the difference between a rendered template and a static asset:
You could build a route for the print.html
:
@app.route('/print')
def print():
return render_template('print.html')
Which would then enable you to url_for('print')
to get the resolved url, and any dynamic components in your print.html
template would get rendered to static html.
Otherwise, if the print.html
is truly static-- just move it to the static
folder, and use the url_for('static', filename='print.html')
as you've indicated.
There's a handy Flask blueprint pattern that's useful if you're just rendering off a bunch of templates and don't need any view logic, see it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With