diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 1a8e17c6..1164e2b8 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -3,48 +3,59 @@ Quickstart ========== -Eager to get started? This page gives a good introduction to Flask. It -assumes you already have Flask installed. If you do not, head over to the -:ref:`installation` section. +Eager to get started? This page gives a good introduction to Flask. +Follow :doc:`installation` to set up a project and install Flask first. A Minimal Application --------------------- -A minimal Flask application looks something like this:: +A minimal Flask application looks something like this: + +.. code-block:: python from flask import Flask + from markupsafe import escape + app = Flask(__name__) - @app.route('/') + @app.route("/") def hello_world(): - return 'Hello, World!' + return f"
Hello, {escape(name)}!
" So what did that code do? -1. First we imported the :class:`~flask.Flask` class. An instance of this - class will be our WSGI application. -2. Next we create an instance of this class. The first argument is the name of - the application's module or package. If you are using a single module (as - in this example), you should use ``__name__`` because depending on if it's - started as application or imported as module the name will be different - (``'__main__'`` versus the actual import name). This is needed so that - Flask knows where to look for templates, static files, and so on. For more - information have a look at the :class:`~flask.Flask` documentation. -3. We then use the :meth:`~flask.Flask.route` decorator to tell Flask what URL - should trigger our function. -4. The function is given a name which is also used to generate URLs for that - particular function, and returns the message we want to display in the - user's browser. +1. First we imported the :class:`~flask.Flask` class. An instance of + this class will be our WSGI application. +2. Next we create an instance of this class. The first argument is the + name of the application's module or package. ``__name__`` is a + convenient shortcut for this that is appropriate for most cases. + This is needed so that Flask knows where to look for resources such + as templates and static files. +3. We then use the :meth:`~flask.Flask.route` decorator to tell Flask + what URL should trigger our function. +4. The function returns the message we want to display in the user's + browser. The default content type is HTML, so HTML in the string + will be rendered by the browser. -Just save it as :file:`hello.py` or something similar. Make sure to not call +.. note:: HTML escaping + + When returning HTML (the default response type in Flask), any user + input rendered in the output must be escaped to protect from + injection attacks. HTML templates in Jinja, introduced later, will + do this automatically. :func:`~markupsafe.escape`, shown above, can + be used manually. It's omitted for brevity in the examples below. + +Save it as :file:`hello.py` or something similar. Make sure to not call your application :file:`flask.py` because this would conflict with Flask itself. -To run the application you can either use the :command:`flask` command or -python's ``-m`` switch with Flask. Before you can do that you need +To run the application, use the :command:`flask` command or +:command:`python -m flask`. Before you can do that you need to tell your terminal the application to work with by exporting the -``FLASK_APP`` environment variable:: +``FLASK_APP`` environment variable: + +.. code-block:: text $ export FLASK_APP=hello.py $ flask run @@ -59,12 +70,6 @@ And on PowerShell:: PS C:\path\to\app> $env:FLASK_APP = "hello.py" -Alternatively you can use :command:`python -m flask`:: - - $ export FLASK_APP=hello.py - $ python -m flask run - * Running on http://127.0.0.1:5000/ - This launches a very simple builtin server, which is good enough for testing but probably not what you want to use in production. For deployment options see :ref:`deployment`. @@ -203,17 +208,17 @@ of the argument like ``