Skip to content
Myr

Fixed various stylistic issues in documentation

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

authored by FND · committed by FND · parent a9726c4 · Browse files · 3 files+79 added−80 removed
This commit

State

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

Diff stats

3 files+79 added−80 removed

  • other:+79 added−80 removed

3 files+79 added−80 removed

modified:docs/foreword.rst+14 added-14 removed
7 unmodified lines
8What does "micro" mean?9-----------------------1011To me, the "micro" in microframework refers not only to the simplicity and12small size of the framework, but also the fact that it does not make much12small size of the framework, but also the fact that it does not make many13decisions for you. While Flask does pick a templating engine for you, we14won't make such decisions for your datastore or other parts.1516For us however the term “micro” does not mean that the whole implementation16However, to us the term “micro” does not mean that the whole implementation17has to fit into a single Python file.1819One of the design decisions with Flask was that simple tasks should be20simple and not take up a lot of code and yet not limit yourself. Because21of that we took a few design choices that some people might find20simple; they should not take a lot of code and yet they should not limit you.21Because of that we made a few design choices that some people might find22surprising or unorthodox. For example, Flask uses thread-local objects23internally so that you don't have to pass objects around from function to24function within a request in order to stay threadsafe. While this is a25really easy approach and saves you a lot of time, it might also cause some
3 unmodified lines
29and provide you with a lot of tools to make it as pleasant as possible to30work with them.3132Flask is also based on convention over configuration, which means that33many things are preconfigured. For example, by convention, templates and34static files are in subdirectories within the Python source tree of the35application. While this can be changed you usually don't have to.33many things are preconfigured. For example, by convention templates and34static files are stored in subdirectories within the application's Python source tree.35While this can be changed you usually don't have to.3637The main reason however why Flask is called a "microframework" is the idea37The main reason Flask is called a "microframework" is the idea38to keep the core simple but extensible. There is no database abstraction39layer, no form validation or anything else where different libraries40already exist that can handle that. However Flask knows the concept of41extensions that can add this functionality into your application as if it40already exist that can handle that. However Flask supports41extensions to add such functionality to your application as if it42was implemented in Flask itself. There are currently extensions for43object relational mappers, form validation, upload handling, various open43object-relational mappers, form validation, upload handling, various open44authentication technologies and more.4546Since Flask is based on a very solid foundation there is not a lot of code47in Flask itself. As such it's easy to adapt even for large applications
22 unmodified lines
7071The documentation will warn you about aspects of web development that72require attention to security. Some of these security concerns73are far more complex than one might think, and we all sometimes underestimate74the likelihood that a vulnerability will be exploited, until a clever74the likelihood that a vulnerability will be exploited - until a clever75attacker figures out a way to exploit our applications. And don't think76that your application is not important enough to attract an attacker.77Depending on the kind of attack, chances are that automated bots are78probing for ways to fill your database with spam, links to malicious
8 unmodified lines
87support the new iteration of the Python programming language. While the88situation is greatly improving there are still some issues that make it89hard for us to switch over to Python 3 just now. These problems are90partially caused by changes in the language that went unreviewed for too91long, partially also because we have not quite worked out how the lower92level API should change for the unicode differences in Python3.91long, partially also because we have not quite worked out how the lower-92level API should change to account for the Unicode differences in Python 3.9394Werkzeug and Flask will be ported to Python 3 as soon as a solution for95the changes is found, and we will provide helpful tips how to upgrade96existing applications to Python 3. Until then, we strongly recommend
4 unmodified lines
modified:docs/installation.rst+9 added-9 removed
7 unmodified lines
8Werkzeug is a toolkit for WSGI, the standard Python interface between web9applications and a variety of servers for both development and deployment.10Jinja2 renders templates.1112So how do you get all that on your computer quickly? There are many ways13which this section will explain, but the most kick-ass method is12So how do you get all that on your computer quickly? There are many ways,13as this section will explain, but the most kick-ass method is14virtualenv, so let's look at that first.1516Either way, you will need Python 2.5 or higher to get started, so be sure17to have an up to date Python 2.x installation. At the time of writing,
39 unmodified lines
57 $ sudo apt-get install python-virtualenv5859If you are on Windows and don't have the `easy_install` command, you must60install it first. Check the :ref:`windows-easy-install` section for more61information about how to do that. Once you have it installed, run the61information on how to do that. Once you have it installed, run the62same commands as above, but without the `sudo` prefix.6364Once you have virtualenv installed, just fire up a shell and create65your own environment. I usually create a project folder and an `env`
11 unmodified lines
77 $ . env/bin/activate7879(Note the space between the dot and the script name. The dot means that80this script should run in the context of the current shell. If this command81does not work in your shell, try replacing the dot with ``source``)81does not work in your shell, try replacing the dot with ``source``.)8283If you are a Windows user, the following command is for you::8485 $ env\scripts\activate
8 unmodified lines
9495A few seconds later you are good to go.969798System Wide Installation98System-Wide Installation99------------------------100101This is possible as well, but I do not recommend it. Just run102`easy_install` with root rights::101This is possible as well, though I do not recommend it. Just run102`easy_install` with root privileges::103104 $ sudo easy_install Flask105106(Run it in an Admin shell on Windows systems and without `sudo`).106(Run it in an Admin shell on Windows systems and without `sudo`.)107108109Living on the Edge110------------------
47 unmodified lines
158to the `PATH` environment variable. To do that, right-click on the159"Computer" icon on the Desktop or in the Start menu, and choose160"Properties". Then, on Windows Vista and Windows 7 click on "Advanced System161settings"; on Windows XP, click on the "Advanced" tab instead. Then click162on the "Environment variables" button and double click on the "Path"162on the "Environment variables" button and double-click on the "Path"163variable in the "System variables" section. There append the path of your164Python interpreter's Scripts folder; make sure you delimit it from165existing values with a semicolon. Assuming you are using Python 2.6 on166the default path, add the following value::
9 unmodified lines
modified:docs/quickstart.rst+56 added-57 removed
1 unmodified line
23Quickstart4==========56Eager to get started? This page gives a good introduction in how to get6Eager to get started? This page gives a good introduction on how to get7started with Flask. This assumes you already have Flask installed. If8you do not, head over to the :ref:`installation` section.910
27 unmodified lines
38So what did that code do?39401. First we imported the :class:`~flask.Flask` class. An instance of this41 class will be our WSGI application. The first argument is the name of42 the application's module. If you are using a single module (like here)43 you should use `__name__` because depending on if it's started as42 the application's module. If you are using a single module (as in this example)43 you should use `__name__` because depending on whether it's started as44 application or imported as module the name will be different45 (``'__main__'`` versus the actual import name). For more information46 on that, have a look at the :class:`~flask.Flask` documentation.46 on this technique, have a look at the :class:`~flask.Flask` documentation.472. Next we create an instance of it. We pass it the name of the module /48 package. This is needed so that Flask knows where it should look for49 templates, static files and so on.503. Then we use the :meth:`~flask.Flask.route` decorator to tell Flask51 what URL should trigger our function.51 which URL should trigger our function.524. The function then has a name which is also used to generate URLs to53 that particular function, and returns the message we want to display in54 the user's browser.555. Finally we use the :meth:`~flask.Flask.run` function to run the
6 unmodified lines
62.. _public-server:6364.. admonition:: Externally Visible Server6566 If you run the server you will notice that the server is only available66 If you run the server you will notice that the server is only accessible67 from your own computer, not from any other in the network. This is the68 default because in debugging mode a user of the application can execute69 arbitrary Python code on your computer. If you have `debug` disabled70 or trust the users on your network, you can make the server publicly71 available.69 arbitrary Python code on your computer.7073 Just change the call of the :meth:`~flask.Flask.run` method to look74 like this::71 If you have `debug` disabled or trust the users on your network, you can72 make the server publicly available simply by changing the call of the73 :meth:`~flask.Flask.run` method to look like this::7475 app.run(host='0.0.0.0')7677 This tells your operating system to listen on a public IP.
3 unmodified lines
81----------8283The :meth:`~flask.Flask.run` method is nice to start a local84development server, but you would have to restart it manually after each86change you do to code. That is not very nice and Flask can do better. If87you enable the debug support the server will reload itself on code changes88and also provide you with a helpful debugger if things go wrong.85change to your code. That is not very nice and Flask can do better. If86you enable debug support the server will reload itself on code changes,87and it will also provide you with a helpful debugger if things go wrong.8889There are two ways to enable debugging. Either set that flag on the90application object::9192 app.debug = True93 app.run()9496Or pass it to run::95Or pass it to `run`::9697 app.run(debug=True)9899Both will have exactly the same effect.
21 unmodified lines
121Routing122-------123124Modern web applications have beautiful URLs. This helps people remember126the URLs which is especially handy for applications that are used from125the URLs, which is especially handy for applications that are used from126mobile devices with slower network connections. If the user can directly127go to the desired page without having to hit the index page it is more128likely they will like the page and come back next time.129
39 unmodified lines
169170.. admonition:: Unique URLs / Redirection Behaviour171172 Flask's URL rules are based on Werkzeug's routing module. The idea174 behind that module is to ensure nice looking and also unique URLs based175 on behaviour Apache and earlier servers coined.173 behind that module is to ensure nice-looking and also unique URLs based174 on behaviour coined by Apache and earlier servers.175176 Take these two rules::177178 @app.route('/projects/')
9 unmodified lines
188 `projects` endpoint has a trailing slash. It's similar to a folder in189 that sense. Accessing it without a trailing slash will cause Flask to190 redirect to the canonical URL with the trailing slash.191193 However in the second case the URL is defined without a slash so it192 However, in the second case the URL is defined without a slash so it193 behaves similar to a file and accessing the URL with a trailing slash195 will be a 404 error.194 will result in a 404 error.195196 Why is this? This allows relative URLs to continue working if users197 access the page when they forget a trailing slash. This behaviour is198 also consistent with how Apache and other servers work. Also, the URLs200 will stay unique which helps search engines not indexing the same page199 will stay unique which ensures search engines do not index the same page200 twice.201202203.. _url-building:
32 unmodified lines
236237(This also uses the :meth:`~flask.Flask.test_request_context` method238explained below. It basically tells Flask to think we are handling a239request even though we are not, we are in an interactive Python shell.241Have a look at the explanation below. :ref:`context-locals`).240Have a look at the explanation below: :ref:`context-locals`.)241242Why would you want to build URLs instead of hardcoding them in your243templates? There are three good reasons for this:244
23 unmodified lines
268 show_the_login_form()269270If `GET` is present, `HEAD` will be added automatically for you. You271don't have to deal with that. It will also make sure that `HEAD` requests273are handled like the `HTTP RFC`_ (the document describing the HTTP272are handled as the `HTTP RFC`_ (the document describing the HTTP273protocol) demands, so you can completely ignore that part of the HTTP275specification. Likewise as of Flask 0.6, `OPTIONS` is implemented for you276as well automatically.274specification. Likewise, as of Flask 0.6, `OPTIONS` is implemented for you275automatically as well.276277You have no idea what an HTTP method is? Worry not, here is a quick278introduction to HTTP methods and why they matter:279
15 unmodified lines
295296`POST`297 The browser tells the server that it wants to *post* some new298 information to that URL and that the server must ensure the data is300 stored and only stored once. This is how HTML forms are usually301 transmitting data to the server.299 stored and only stored once. This is how HTML forms usually300 transmit data to the server.301302`PUT`303 Similar to `POST` but the server might trigger the store procedure304 multiple times by overwriting the old values more than once. Now you306 might be asking why is this useful, but there are some good reasons307 to do it this way. Consider that the connection gets lost during305 might be asking why this is useful, but there are some good reasons306 to do it this way. Consider that the connection is lost during307 transmission: in this situation a system between the browser and the308 server might receive the request safely a second time without breaking309 things. With `POST` that would not be possible because it must only310 be triggered once.
17 unmodified lines
328329Static Files330------------331333Dynamic web applications need static files as well. That's usually where332Dynamic web applications also need static files. That's usually where333the CSS and JavaScript files are coming from. Ideally your web server is334configured to serve them for you, but during development Flask can do that335as well. Just create a folder called `static` in your package or next to336your module and it will be available at `/static` on the application.337339To generate URLs to that part of the URL, use the special ``'static'`` URL338To generate URLs that part of the URL, use the special ``'static'`` URL339name::340341 url_for('static', filename='style.css')342
7 unmodified lines
350the application secure. Because of that Flask configures the `Jinja2351<http://jinja.pocoo.org/2/>`_ template engine for you automatically.352353To render a template you can use the :func:`~flask.render_template`355method. All you have to do is to provide the name of the template and the354method. All you have to do is provide the name of the template and the355variables you want to pass to the template engine as keyword arguments.356Here's a simple example of how to render a template::357358 from flask import render_template
3 unmodified lines
362 def hello(name=None):363 return render_template('hello.html', name=name)364365Flask will look for templates in the `templates` folder. So if your367application is a module, that folder is next to that module, if it's a366application is a module, this folder is next to that module, if it's a367package it's actually inside your package:368369**Case 1**: a module::370
32 unmodified lines
403know how that works, head over to the :ref:`template-inheritance` pattern404documentation. Basically template inheritance makes it possible to keep405certain elements on each page (like header, navigation and footer).406408Automatic escaping is enabled, so if name contains HTML it will be escaped407Automatic escaping is enabled, so if `name` contains HTML it will be escaped408automatically. If you can trust a variable and you know that it will be410safe HTML (because for example it came from a module that converts wiki409safe HTML (for example because it came from a module that converts wiki410markup to HTML) you can mark it as safe by using the411:class:`~jinja2.Markup` class or by using the ``|safe`` filter in the412template. Head over to the Jinja 2 documentation for more examples.413
26 unmodified lines
440For web applications it's crucial to react to the data a client sent to441the server. In Flask this information is provided by the global442:class:`~flask.request` object. If you have some experience with Python443you might be wondering how that object can be global and how Flask445manages to still be threadsafe. The answer are context locals:444manages to still be threadsafe. The answer is context locals:445446447.. _context-locals:448
9 unmodified lines
458These objects are actually proxies to objects that are local to a specific459context. What a mouthful. But that is actually quite easy to understand.460461Imagine the context being the handling thread. A request comes in and the463webserver decides to spawn a new thread (or something else, the464underlying object is capable of dealing with other concurrency systems465than threads as well). When Flask starts its internal request handling it462web server decides to spawn a new thread (or something else, the463underlying object is capable of dealing with concurrency systems other464than threads). When Flask starts its internal request handling it465figures out that the current thread is the active context and binds the466current application and the WSGI environments to that context (thread).468It does that in an intelligent way that one application can invoke another467It does that in an intelligent way so that one application can invoke another468application without breaking.469470So what does this mean to you? Basically you can completely ignore that472this is the case unless you are doing something like unittesting. You473will notice that code that depends on a request object will suddenly break471this is the case unless you are doing something like unit testing. You472will notice that code which depends on a request object will suddenly break473because there is no request object. The solution is creating a request474object yourself and binding it to the context. The easiest solution for476unittesting is by using the :meth:`~flask.Flask.test_request_context`475unit testing is to use the :meth:`~flask.Flask.test_request_context`476context manager. In combination with the `with` statement it will bind a477test request so that you can interact with it. Here is an example::478479 from flask import request
198 unmodified lines
6782. If it's a string, a response object is created with that data and the679 default parameters.6803. If a tuple is returned the response object is created by passing the681 tuple as arguments to the response object's constructor.6834. If neither of that works, Flask will assume the return value is a684 valid WSGI application and converts that into a response object.6824. If none of that works, Flask will assume the return value is a683 valid WSGI application and convert that into a response object.684685If you want to get hold of the resulting response object inside the view686you can use the :func:`~flask.make_response` function.687
21 unmodified lines
709710Sessions711--------712714Besides the request object there is also a second object called715:class:`~flask.session` that allows you to store information specific to a713In addition to the request object there is also a second object called714:class:`~flask.session` which allows you to store information specific to a715user from one request to the next. This is implemented on top of cookies716for you and signs the cookies cryptographically. What this means is that717the user could look at the contents of your cookie but not modify it,718unless they know the secret key used for signing.
31 unmodified lines
750751 # set the secret key. keep this really secret:752 app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'753755The here mentioned :func:`~flask.escape` does escaping for you if you are756not using the template engine (like in this example).754The :func:`~flask.escape` mentioned here does escaping for you if you are755not using the template engine (as in this example).756757.. admonition:: How to generate good secret keys758760 The problem with random is that it's hard to judge what random is. And759 The problem with random is that it's hard to judge what is truly random. And760 a secret key should be as random as possible. Your operating system761 has ways to generate pretty random stuff based on a cryptographic762 random generator which can be used to get such a key:763
9 unmodified lines
773Good applications and user interfaces are all about feedback. If the user774does not get enough feedback they will probably end up hating the775application. Flask provides a really simple way to give feedback to a776user with the flashing system. The flashing system basically makes it778possible to record a message at the end of a request and access it next779request and only next request. This is usually combined with a layout780template that does this.777possible to record a message at the end of a request and access it on the next778(and only the next) request. This is usually combined with a layout779template to expose the message.780781To flash a message use the :func:`~flask.flash` method, to get hold of the782messages you can use :func:`~flask.get_flashed_messages` which is also783available in the templates. Check out the :ref:`message-flashing-pattern`
4 unmodified lines
788789.. versionadded:: 0.3790791Sometimes you might be in a situation where you deal with data that793should be correct, but actually is not. For example you may have some client794side code that sends an HTTP request to the server but it's obviously795malformed. This might be caused by a user tempering with the data, or the796client code failing. Most of the time, it's okay to reply with ``400 Bad792should be correct, but actually is not. For example you may have some client-side793code that sends an HTTP request to the server but it's obviously794malformed. This might be caused by a user tampering with the data, or the795client code failing. Most of the time it's okay to reply with ``400 Bad796Request`` in that situation, but sometimes that won't do and the code has797to continue working.798799You may still want to log that something fishy happened. This is where
24 unmodified lines