Difference between render_template and redirect?

Question:

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?

Asked By: onepiece

||

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.

Answered By: 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.

Answered By: farthVader

One URL endpoint can have many different render_template statements and thus render different page templates from your templates folder. On the other hand, using a redirect changes the URL itself and thus calls the methods associated to that route.

Answered By: JSON_Derulo

using redirect() for redirecting to a passed URL is equivalent to passing a GET request from the client-side(browser) for the URL passed in the redirect() method, because of which it leads to the execution of the "view" function of the passed URL, where generally, in the end, we have a "return statement", returning the Html page(template) via render_template, which ultimately passed the Html page to the client browser.

whereas render_template only pass the Html page to the client browser, with its all other parameters, and no execution of the "view" function takes place after that.

Answered By: bhanu pratap

The important thing is consistency, always do the following:

  • "GET" method use render_template ("index.html")
  • "POST" method use redirect("/")

Even though (in this example) both will show the same web page, the dynamic data within may be different due to different methods. As we know, GET is as simple as a click in a hyperlink on web page while POST is submitting web form which may involve calculation of data and may show in the final web page.

Another supporting recommendation from "Flask Web Development" book by Miguel Grinberg (O’Reilly, 2018):
If you submit a form (via POST method), if user refresh the page, an obscure warning will pop-up to ask confirmation before submitting the form again. This happens because browser repeat the last request, and in this case POST. So it is a good practice to never leave a POST request as the last request sent by browser. A ‘redirect’ issues a GET request for the redirect URL, and that is the page that it displays. Now the last request is a GET, so the refresh click works as expected, this good practice is known as POST/Redirect/GET pattern. So always use ‘redirect’ for POST method.

Answered By: KokoEfraim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.