Erik Nagy
Erik Nagy

Reputation: 31

How can I pass a python variable to my template via flask?

I made a web application with flask. I want to pass a varable value to my template, to set property.

in python:

web_size="100%"

@app.route('/')
def home():
  return render_template("web.html")

def size():
  return "50px"

In the html5 file, I used that:

width: {{url_for('size')}};

what did i wrong? and how can I use orl_for command? I think I didn't even understand how it works.

Upvotes: 0

Views: 1322

Answers (2)

Luke
Luke

Reputation: 2901

To pass a Python variable to a Flask template do the following:

In the view first declare the Python variable. Then, pass the variable to the template as a parameter in your render_template method:

@app.route('/', methods={'GET'})
def home():
    title = "Frankenstein"
    return render_template('index.html', book_title=title )

Then, to display the variable in your index.html template use curly braces:

<h3>The book named "{{book_title}}" </h3>

Upvotes: 2

olijeffers0n
olijeffers0n

Reputation: 93

You haven't passed the data into the html sheet. For example:

message = 'Hello!'
return render_template('index.html', value= message)

Upvotes: 1

Related Questions