0

I have a very basic question (first time i use Flask, i am not used to web frameworks).

I would simply like to update variables in a template e.g. hello.html nothing else (i don't care about routes or anything at the moment)

i do that according to a tutorial:

from flask import Flask, render_template app = Flask(__name__) x=1986 res=render_template('hello.html', myVar = x) print(res) if __name__ == '__main__': app.run(debug = True) 

Please note that i have a html file name hello.html in a subdir called templates. hello.html:

<!doctype html> <html> <body> <h1>Hello {% print(myVar) %}</h1> </body> </html> 

what am i doing wrong?

edit. i get this error message:

Traceback (most recent call last): File "flask_test1.py", line 7, in <module> res=render_template('hello.html', myVar = x) File "/root/miniconda3/lib/python3.4/site-packages/flask/templating.py", line 133, in render_template ctx.app.update_template_context(context) AttributeError: 'NoneType' object has no attribute 'app' 

P.s. sorry about my "clumsyness" with web frameworks...

2 Answers 2

2

It is impossible that your code structure would work. The basic concept in flask is context. Normally you can think that is a complete scope for a single request.

So as you can see in your error message, ctx which is context is None. You cannot use render_template like this.

Instead, you can directly use the underlying render engine used by flask: jinja2.

from jinja2 import Environment, FileSystemLoader, select_autoescape env = Environment( loader=FileSystemLoader('your/templates/dir'), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template('hello.html') x = 1986 print(template.render(myVar = x)) 

And your template should be:

<!doctype html> <html> <body> <h1>Hello {{ myVar }}</h1> </body> </html> 
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic Thanks ! this is exactly what i was looking for! you really save me some time.
Wow amazing answer .For this T had spend 2 days .Thanks a lot
2

You don't say what tutorial you're following but I'm pretty sure it didn't show that structure.

You need to put your code within a function and decorate it with the URL you want to use; and then you need to return the rendered template from the function, rather than printing it.

@app.route("/") def index(): x = 1986 res = render_template('hello.html', myVar=x) return res 

Also, you don't use print() within a template; to output the value of a variable you use {{ }} instead of {% %}.

<h1>Hello {{ myVar }}</h1> 

1 Comment

thanks, yes indeed there was this kind of structure (that you gave in the tutorial). It runs without error, however what i want is simply to get the actual text content , i.e. res from within my code , like resOutside=index() and then print(resOutside) but this fails

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.