Flask render_template with url parameter

I am using pdf.js to render pdf on the web. The format of the destination URL is as follows:

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

My problem: I am using a bulb template to create a page with:

 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), which uses it to read the file for pdf rendering.

I'm new to the flask, hoping the guys will help me!

0
flask templates pdfjs
Nov 17 '17 at 8:21
source share
3 answers

You can use the redirect function, which can redirect you what 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) 
0
Nov 21 '17 at 15:01
source share

You can get the parameter that was passed in the request URL using the request object.

  @app.route("/book", methods=["GET"]) def get_book(): file = request.args.get("file") if file: return render_template('book.html', file=file) abort(401, description="Missing file parameter in request url") 
0
Nov 17 '17 at 10:50
source share

This section has helped me change my question, but it doesn't seem to be the perfect answer. viewer.py code is not changed. Decision:

step1) I embed the code

 <script>var FILE_PATH = {{ file }}</script> 

in the template.

step2) the script that will use the variable, you need to change the code ( viewer.js ), from:

 var file = 'file' in params ? params.file : DEFAULT_URL 

to

 var file = FILE_PATH ? FILE_PATH: DEFAULT_URL 

It allows viewer.js to no longer be independent.

I hope someone will provide a better solution.

0
Nov 18 '17 at 15:26
source share



All Articles