flask subdomain variable not captured, other routes 404

  • Last Update :
  • Techknowledgy :

You need to include the port number in your SERVER_NAME config.

app.config['SERVER_NAME'] = 'example.com:5000'

Suggestion : 2

Answer link : https://codehunter.cc/a/flask/flask-subdomain-variable-not-captured-other-routes-404,For the 'index' route the subdomain parameter doesn't get captured when I browse to http://blog.example.com:5000. When I try to print var it prints "var is ".,I'm trying to use Flask's subdomain parameter, but having some trouble.,r/codehunterTrigger $document.ready (so AJAX code I can't modify is...100%02h

The 'login' route 404s, but I can't figure out why. Any help would be greatly appreciated!

from flask import Flaskapp = Flask(__name__)app.debug=Trueapp.config['SERVER\_NAME'] = 'example.com'# prints "var is <invalid>"@app.route('/', subdomain="<var>", methods=['GET'])def index(var): print "var is %s" % var return "Hello World %s" % var# This 404s@app.route('/login/', methods=['GET'])def login(): return "Login Here!"if __name__ == '\_\_main\_\_': app.run(host='example.com', debug=True)

Suggestion : 3

The endpoint name for the route defaults to the name of the view function if the endpoint parameter isn’t passed. An error will be raised if a function has already been registered for the endpoint.,The endpoint name for the route defaults to the name of the view function if the endpoint parameter isn’t passed.,the endpoint for the registered URL rule. Flask itself assumes that the name of the view function is the name of the endpoint if not explicitly stated.,view_func does not necessarily need to be passed, but if the rule should participate in routing an endpoint name must be associated with a view function at some point with the endpoint() decorator.

from flask
import Flask
app = Flask(__name__)
app = Flask('yourapplication')
app = Flask(__name__.split('.')[0])
@app.route("/")
def index():
   ...
def index():
   ...

   app.add_url_rule("/", view_func = index)
app.add_url_rule("/", endpoint = "index")

@app.endpoint("index")
def index():
   ...
with app.app_context():
   init_db()

Suggestion : 4

Last Updated : 15 Oct, 2020

For this, we need to download and import flask. Download the flask through the following commands on CMD.

pip install flask

Suggestion : 5

 March 24, 2022     flask, python     No comments   

Directory structure:

server
   -
   > user -
   > __init__.py -
   > models.py -
   > routes.py -
   > app.py -
   > __init__.py

app.py:

from flask import Flask

app = Flask(__name__)

from user import routes

@app.route('/') # THIS ROUTE WORKS
def index():
return '<h1>HOME</h1>'

if __name__ == '__main__':
app.run(debug = True)

routes.py:

from app import app

@app.route('/register', methods=['GET']) # THIS DOESN'T WORK - 404 NOT FOUND ERROR
def register_user():
return '<h1>Register a user!</h1>'

user/routes.py:

from flask import Blueprint, render_template_string
from user.models import User # if I comment this, it starts working...

bp = Blueprint('user', __name__)

@bp.route('/register', methods=['POST'])
def register_user():
user = User()
user.register()
return user

@bp.route('/register', methods=['GET'])
def check_user():
return render_template_string('<h1>User Registered</h1>')

user/models.py:

import uuid
from urllib
import request
from flask
import jsonify
from app
import db # fishy...

   class User(object):
   def register(self):
   user = {
      '_id': uuid.uuid4().hex,
      'email': request.form.get('email'),
      'password': request.form.get('password'),
   }
db.users.insert_one(user):
   return jsonify(user), 200

Suggestion : 6

Paths can use URI templating style to capture path components.,You'll often want to be able to generate the URL for a particular route, such as in cases where you need to return a redirect response.,In cases where more that one route could match an incoming path, you should take care to ensure that more specific routes are listed before general cases.,Path parameters, and reverse URL lookups for WebSocketRoute work the the same as HTTP Route, which can be found in the HTTP Route section above.

from starlette.applications
import Starlette
from starlette.responses
import PlainTextResponse
from starlette.routing
import Route

async def homepage(request):
   return PlainTextResponse("Homepage")

async def about(request):
   return PlainTextResponse("About")

routes = [
   Route("/", endpoint = homepage),
   Route("/about", endpoint = about),
]

app = Starlette(routes = routes)
Route('/users/{username}', user)
Route('/users/{user_id:int}', user)
Route('/floating-point/{number:float}', floating_point)
Route('/uploaded/{rest_of_path:path}', uploaded)
from datetime
import datetime

from starlette.convertors
import Convertor, register_url_convertor

class DateTimeConvertor(Convertor):
   regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?"

def convert(self, value: str) - > datetime:
   return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")

def to_string(self, value: datetime) - > str:
   return value.strftime("%Y-%m-%dT%H:%M:%S")

register_url_convertor("datetime", DateTimeConvertor())
Route('/history/{date:datetime}', history)
async def user(request):
   user_id = request.path_params['user_id']
   ...