How to pass a python variable to html in flask without rendering template

Viewed 3277

So, I have been trying to make a very basic flask website, just for the sake of learning flask, and I ran into a problem. When trying to pass a variable to HTML code from python, I am aware you could just do this:

from flask import Flask,request,render_template,redirect,url_for
app=Flask(__name__)
@app.route("/",methods=["POST","GET","DELETE"])
def home():
    x="a variable"
    return render_template("index.html",htmlvar=x)

But, my issue is that my variable comes from a text input from the HTML, so I can't get it without rendering the HTML template, which is why I need to know a way to pass a variable after rendering the template. Any help would be greatly appreciated.

1 Answers

I think this is already answered in the link mentioned by Joe above. But just to re-iterate that.

use the return as :

return render_template('display.html', html_value=some_program_value)

Where the display.html should be like

<!doctype html>
    <body>

   <div> {{ html_value }} </div>

   </body>
   </html>
Related