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.

2
  • 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? Commented Apr 11, 2020 at 2:56
  • Does this answer your question? How to pass variables between HTML pages using Flask Commented Apr 11, 2020 at 5:33

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.