Dynamic Subdomain Handling in a Web App (Flask) [closed]
Asked Answered
N

2

40

I'm going to be using flask to create a web application, and part of the application will involve a subdomain (for example, user1.appname.org).

I'm not sure how to go about creating these subdomains dynamically in the flask configuration, or how to deploy them to a production server.

What is the best way of doing this?

Noctiluca answered 13/6, 2012 at 19:8 Comment(0)
G
69

All Flask's routing constructs support the subdomain keyword argument (this includes support for route variables).

@app.route("/", subdomain="static")
def static_index():
    """Flask supports static subdomains
    This is available at static.your-domain.tld"""
    return "static.your-domain.tld"

@app.route("/dynamic", subdomain="<username>")
def username_index(username):
    """Dynamic subdomains are also supported
    Try going to user1.your-domain.tld/dynamic"""
    return username + ".your-domain.tld"
Gallows answered 14/6, 2012 at 5:22 Comment(8)
Will this work running on the Flask testing localhost server?Noctiluca
I'm also wondering if there's a way to test this locally. I've tried adding serveral entries to my hosts file., this doesn't seem to work.Archival
@sean How woud you do something similar for custom domains. A user signs up with subdomain.example.com and then adds a custom domain such as www.mygreatsite.com ? How would I handle access to this custom domain in a Flask route?Birdcage
@Birdcage - that would depend on how you are setting up those custom domains. (Better to ask a separate question with more details about your setup.)Gallows
You should also be able to test this locally with a free service like xip.io by 37SignalsResonator
Downvoter, care to explain so I can improve the answer?Gallows
@SeanVieira, do you have a boiler-plate for this setup somewhere. i need one.Sleek
How does this work with class based views?Commixture
A
54

To complement Sean Viera's post, you also need to set the SERVER_NAME config variable.

Documentation: http://flask.pocoo.org/docs/config/#SERVER_NAME

The name and port number of the server. Required for subdomain support (e.g.: 'myapp.dev:5000') Note that localhost does not support subdomains so setting this to “localhost” does not help. Setting a SERVER_NAME also by default enables URL generation without a request context but with an application context.

To test locally you need to add entries to your hosts file, like this:

127.0.0.1       cvshark.local
127.0.0.1       robert.cvshark.local
127.0.0.1       www.cvshark.local
Archival answered 1/7, 2012 at 11:50 Comment(5)
I don't understand the example they give: 'myapp.dev:5000'. Is myapp the subdomain and dev the hostname?Nordin
@Nordin Yes, that's correct.Archival
This is the better answer; it goes beyond the documentation and solves the trickier stuff.Pronounced
but can you add DYNAMIC entries to the host file?Sideling
@MichaelIlie Yes you can, look at the last route in Sean Vieira answer.Breaking

© 2022 - 2024 — McMap. All rights reserved.