Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between render_template and redirect?

Tags:

python

flask

return redirect(url_for('index', var=var)) return render_template('index.html', var=var) 

Are these two lines essentially the same thing?

What is the difference between the two functions?

like image 414
onepiece Avatar asked Feb 10 '14 03:02

onepiece


People also ask

What is Render_template?

render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.

What is the difference between render and redirect in Django?

The render function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. You request a page and the render function returns it. The redirect function sends another request to the given url.


2 Answers

redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index.html template returned as the content at that URL.

like image 191
pswaminathan Avatar answered Sep 30 '22 16:09

pswaminathan


On a much simpler note, consider this - If none of your endpoints rendered templates, and all your redirects were to url's of endpoints within your app, there would be nothing to render!

It's like pointing the way to a place that won't show itself.

like image 35
farthVader Avatar answered Sep 30 '22 15:09

farthVader