Skip to content
Myr

Move HTML escaping example back to Variable Rules.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Demonstration of markupsafe's escape function was in the Minimal Application example, but the minimal example does not accept user input.

authored by Peter G Kritikos · committed by Peter Kritikos · parent 64ba434 · Browse files · 1 file+13 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+13 added−13 removed

  • other:+13 added−13 removed

1 file+13 added−13 removed

modified:docs/quickstart.rst+13 added-13 removed
13 unmodified lines
1415.. code-block:: python1617 from flask import Flask18 from markupsafe import escape1819 app = Flask(__name__)2021 @app.route("/")22 def hello_world():24 return f"<p>Hello, {escape(name)}!</p>"23 return "<p>Hello, World!</p>"2425So what did that code do?26271. First we imported the :class:`~flask.Flask` class. An instance of
8 unmodified lines
364. The function returns the message we want to display in the user's37 browser. The default content type is HTML, so HTML in the string38 will be rendered by the browser.3941.. note:: HTML escaping4243 When returning HTML (the default response type in Flask), any user44 input rendered in the output must be escaped to protect from45 injection attacks. HTML templates in Jinja, introduced later, will46 do this automatically. :func:`~markupsafe.escape`, shown above, can47 be used manually. It's omitted for brevity in the examples below.4840Save it as :file:`hello.py` or something similar. Make sure to not call41your application :file:`flask.py` because this would conflict with Flask42itself.43
156 unmodified lines
200201 @app.route('/user/<username>')202 def show_user_profile(username):203 # show the user profile for that user213 return f'User {username}'204 return f'User {escape(username)}'205206 @app.route('/post/<int:post_id>')207 def show_post(post_id):208 # show the post with the given id, the id is an integer218 return f'Post {post_id}'209 return f'Post {escape(post_id)}'210211 @app.route('/path/<path:subpath>')212 def show_subpath(subpath):213 # show the subpath after /path/223 return f'Subpath {subpath}'214 return f'Subpath {escape(subpath)}'215216Converter types:217218========== ==========================================
3 unmodified lines
222``path`` like ``string`` but also accepts slashes223``uuid`` accepts UUID strings224========== ==========================================225226.. note:: HTML escaping227228 When returning HTML (the default response type in Flask), any user229 input rendered in the output must be escaped to protect from230 injection attacks. HTML templates in Jinja, introduced later, will231 do this automatically. :func:`~markupsafe.escape`, shown above, can232 be used manually.233234235Unique URLs / Redirection Behavior236``````````````````````````````````237238The following two rules differ in their use of a trailing slash. ::
664 unmodified lines