Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

in php there is

header('Refresh: 1;url=destination');

which redirects the client to a given url, and while it must be called before anything else, it allows me to display something else after that line.

In Flask + python which I am practicing, there is

return redirect(destination, code=301)

which also redirects, but isn't able to do anything else, like displaying further code to the client as PHP can do.

If I'm not wrong, both functions send an header which says to the client to go to another location, but they behave differently: in the first case it is sent while providing an answer; in the second case, it is the answer.

I'd like to have the same (php) function in Flask but after reading the docs, I guess there isn't a way to serve a redirect together with some content.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

All header() does is add a HTTP header to the response. You can do so in Flask too.

Either return a tuple of (body, status, headers) or return a flask.Response() object. See About Responses in the quickstart.

Example with a body, status and headers tuple:

body = render_template('sometemplate', somevar=somevalue)
return (body, 200, {'Refresh': '1;url=destination'})

Here the second element sets a 200 status code (success), and the third is a dictionary specifying the Refresh header.

share|improve this answer
    
Solved perfectly. Thanks Martijn –  user1348293 23 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.