flask render_template with url parameter

jayzhen picture jayzhen · Nov 17, 2017 · Viewed 8.6k times · Source

I use pdf.js to render pdf in web. The format of the target url like this:

http://www.example.com/book?file=abc.pdf

My problem is: I use flask template to generate page using:

return render_template('book.html', paraKey=paraValue)

But how to attach url parameter "file=abc.pdf" to url? The parameter will be read by viewer.js(included in book.html) that uses it to read file for rendering pdf.

I'm new to flask, hoping guys giving me some help!

Answer

Ivan Bryzzhin picture Ivan Bryzzhin · Nov 21, 2017

You could use redirect function, which can redirect you whatever you want:

@app.route('/book')
def hello():
    file = request.args.get('file')
    if not file:
        return redirect("/?file=abc.pdf")
    return render_template('book.html', paraKey=paraValue)