How might I go about implementing subdomains for my website with Flask?
The documentation, though very good, isn't very clear at all about this. The subdomains need not to be dynamic, I am only going to be using 2 or 3 of my own choosing.
How would I route them? Is it possible to test them in the normal way? (served locally by Flask)
DNS ensures the internet is not only user-friendly but also works smoothly, loading whatever content we ask for quickly and efficiently. It's one of the cornerstones of how the internet operates. Without it, we'd be stuck memorizing long lists of numbers (IP addresses) to access the content we want.
Just click the "Add a new web app" button on the Web tab. In the first step, it will ask you for the domain name to use. You should specify the fully-qualified domain name -- that is, www.yourdomain.com , not just yourdomain.com .
It is simple: 1) Go to this website : https://www.whatismyip.com/ ->find your IP address. After the above step restart your server, and try to hit your Flask Server remotely, it should work .
If all you want to do is handle having particular endpoints under a particular subdomain, you can just use the subdomain
argument to @route
:
app = Flask(__name__)
# In Flask 1.0
# app = Flask(__name__, subdomain_matching=True)
# Must add this until Flask 1.0
# Must be host:port pair or will not work
app.config["SERVER_NAME"] = "local.dev:5000"
@app.route("/")
def home():
return "Sweet home"
@app.route("/some-route")
def some_route():
return "on the default subdomain (generally, www, or unguarded)"
@app.route("/", subdomain="blog")
def blog_home():
return "Sweet blog"
@app.route("/<page>", subdomain="blog")
def blog_page(page):
return "can be dynamic: {}".format(page)
To handle development locally, you need to create entries in your hosts
file to point these various domains to your machine:
local.dev 127.0.0.1
blog.local.dev 127.0.0.1
Then you can use local.dev
and blog.local.dev
rather than localhost
to see your work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With