I use Python Flask for my website and I pass several parameters to Javascript. This is my code:
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html", param1="Hello")
<html>
<head>
</head>
<body>
<p>Hello World</p>
</body>
<script>console.log({{param1}})</script>
</html>
With this way, it works without a problem. The example is a simplified of my own. But, if I want to have the script on an external file and call it like this:
<html>
<head>
</head>
<body>
<p>Hello World</p>
</body>
<script src="/static/js/myjs.js"></script>
</html>
And the myjs.js
file is the console.log({{param1}})
, then it doesn't work. So, is there any way to pass parameters in external Javascript files with Python Flask?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…