b9703
b9703

Reputation: 107

heroku hosted flask app redirects to localhost

i currently have a flask app hosted on Horoku. When i visit the page it opens just as it should (renders a html page). However whenever i perform an action that redirects the user (e.g. submit a form) to another directory of the app it tries to redirect to local host and fails (it works if i manually change the url in the url bar). I feel like there's a very simple fix to this but nothing i do seems to make a difference. here's an example...

@app.route('/')
def message_page():
    return(render_template('message_page.html')) 

@app.route('/message_receiver', methods = ['POST'])
def message_reciever():
    message  = request.form['msg']

    ***some other stuff***

    return(redirect(url_for('message_page')))

i tried specifying the host as 0.0.0.0 in both the app.route() parts and the app.run() at the bottom of the app but that made no difference. Note: this works perfectly well when running on localhost (i suppose it masks the problem). I figure the problem must be to do with the url_for() function but i dont know how to fix it.

Upvotes: 1

Views: 1328

Answers (2)

Akmal
Akmal

Reputation: 1

Well, according to the help page, Heroku doesn't redirect. So any redirects are performed by your app.

Upvotes: 0

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

I don't have experience with flask, but docs say that you can pass _external=True to url_for method. This will generate absolute url and you can control actual host with SERVER_NAME environment variable. So you should it set it up on heroku:

heroku config:set SERVER_NAME=your_host

Upvotes: 2

Related Questions