10

I'm currently working on a project using Flask and Google App Engine. Calling get_flashed_messages() returns empty when I flash a message then use a redirect():

@views.route('/todo/add', methods=["POST"])
def add_todo():
    flash('hey')
    return redirect(url_for('todo_list')) 

However, if I comment out

# SERVER_NAME = 'localhost'

then it seems to work fine. My problem is that I have to use subdomains so I need SERVER_NAME to be set.

What is the deal?

Paco
  • 4,520
  • 3
  • 29
  • 53
rgbrgb
  • 1,146
  • 1
  • 11
  • 17

2 Answers2

10

I got it! The trick is to set server name to something with dots.

So 'localhost' became 'app.local' and app.local should be added to /etc/hosts, pointing to the same address as localhost.

From the docs:

Please keep in mind that not only Flask has the problem of not knowing what subdomains are, your web browser does as well. Most modern web browsers will not allow cross-subdomain cookies to be set on a server name without dots in it. So if your server name is 'localhost' you will not be able to set a cookie for 'localhost' and every subdomain of it. Please chose a different server name in that case, like 'myapplication.local' and add this name + the subdomains you want to use into your host config or setup a local bind.

ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
rgbrgb
  • 1,146
  • 1
  • 11
  • 17
  • I found that just commenting out SERVER_NAME got it to work. I previously had `SERVER_NAME = 127.0.0.1:5000`. – Eddy Dec 04 '15 at 18:21
  • Side note, I need to change .dev to .local in my environment. – anvd Dec 02 '16 at 23:55
3

did you set up cookies to work across all subdomains?

by default they are only readable on the domain that set them

  • Thanks for your response. Any idea how to do this? I see that you can call set_cookie() but I don't really know where I would do that... http://flask.pocoo.org/docs/api/#response-objects – rgbrgb Jun 01 '11 at 06:48