How can I get the named parameters from a URL using Flask?
Use request.args to get parsed contents of query string: from flask import request @app.route(…) def login(): username = request.args.get(‘username’) password = request.args.get(‘password’)
Use request.args to get parsed contents of query string: from flask import request @app.route(…) def login(): username = request.args.get(‘username’) password = request.args.get(‘password’)
Your input doesn’t have a name attribute. That is what the client will pass along to the server. Flask will raise a 400 error if you access a form key that wasn’t submitted. <input name=”my_input” id=”my_input” type=”text” value=”{{ email }}”>
You can put your routes in a blueprint: bp = Blueprint(‘burritos’, __name__, template_folder=”templates”) @bp.route(“https://stackoverflow.com/”) def index_page(): return “This is a website about burritos” @bp.route(“/about”) def about_page(): return “This is a website about burritos” Then you register the blueprint with the application using a prefix: app = Flask(__name__) app.register_blueprint(bp, url_prefix=’/abc/123′)
This is answered in the quickstart of the docs. You want a variable URL, which you create by adding <name> placeholders in the URL and accepting corresponding name arguments in the view function. @app.route(‘/landingpage<id>’) # /landingpageA def landing_page(id): … More typically the parts of a URL are separated with /. @app.route(‘/landingpage/<id>’) # /landingpage/A def landing_page(id): … Read more
In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it’s written in. So you would use the relative path nltk_data/ even from a blueprint, or you … Read more
You can stream data in a response, but you can’t dynamically update a template the way you describe. The template is rendered once on the server side, then sent to the client. One solution is to use JavaScript to read the streamed response and output the data on the client side. Use XMLHttpRequest to make … Read more
Running the app in development mode will show an interactive traceback and console in the browser when there is an error. To run in development mode, set the FLASK_ENV=development environment variable then use the flask run command (remember to point FLASK_APP to your app as well). For Linux, Mac, Linux Subsystem for Windows, Git Bash … Read more
Everything inside the {{ … }} is a Python-like expression. You don’t need to use another {{ … }} inside that to reference variables. Drop the extra brackets: <h1>you uploaded {{ name }}<h1> <a href=”https://stackoverflow.com/questions/32024551/{{ url_for(“moremagic’, filename=name) }}”>Click to see magic happen</a> (Note that the url_for() function takes the endpoint name, not a URL path; … Read more
The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library that supplies Flask with the development server when you call app.run(). See the restart_with_reloader() function code; your script is run again with subprocess.call(). If you set use_reloader to False you’ll see the … Read more
If you want to pass some python value around that the user doesn’t need to see or have control over, you can use the session: @app.route(‘/a’) def a(): session[‘my_var’] = ‘my_value’ return redirect(url_for(‘b’)) @app.route(‘/b’) def b(): my_var = session.get(‘my_var’, None) return my_var The session behaves like a dict and serializes to JSON. So you can … Read more