Skip to content
Myr

Merge pull request #3529 from pgkrit/master

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

Move HTML escaping example back to Variable Rules.

authored by David Lord · committed by GitHub · parents 037ae3f, 57f9623 · Browse files · 1 file+30 added−10 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+30 added−10 removed

  • other:+30 added−10 removed

1 file+30 added−10 removed

modified:docs/quickstart.rst+30 added-10 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
123 unmodified lines
167168Have another debugger in mind? See :ref:`working-with-debuggers`.169170171HTML Escaping172-------------173174When returning HTML (the default response type in Flask), any175user-provided values rendered in the output must be escaped to protect176from injection attacks. HTML templates rendered with Jinja, introduced177later, will do this automatically.178179:func:`~markupsafe.escape`, shown here, can be used manually. It is180omitted in most examples for brevity, but you should always be aware of181how you're using untrusted data.182183.. code-block:: python184185 from markupsafe import escape186187 @app.route("/<name>")188 def hello(name):189 return f"Hello, {escape(name)}!"190191If a user managed to submit the name ``<script>alert("bad")</script>``,192escaping causes it to be rendered as text, rather than running the193script in the user's browser.194195``<name>`` in the route captures a value from the URL and passes it to196the view function. These variable rules are explained below.197198199Routing200-------201202Modern web applications use meaningful URLs to help users. Users are more
47 unmodified lines
250``path`` like ``string`` but also accepts slashes251``uuid`` accepts UUID strings252========== ==========================================253254255Unique URLs / Redirection Behavior256``````````````````````````````````257258The following two rules differ in their use of a trailing slash. ::
664 unmodified lines