2

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.

Malhar Khisty
  • 41
  • 1
  • 8
  • If you need to perform an action after the page has already been loaded, you have 2 options: you can either use a form to submit the value and reload the page, or you could use Javascript to perform some function client-side. Are you trying to create a form? – vulpxn Apr 11 '20 at 02:56
  • Does this answer your question? [How to pass variables between HTML pages using Flask](https://stackoverflow.com/questions/51726922/how-to-pass-variables-between-html-pages-using-flask) – Joe Apr 11 '20 at 05:33

1 Answers1

2

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>