Skip to content
Myr

views.rst

This file

Path

docs/tutorial/views.rst

98 lines · 3.8 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoptione20534d

History of this file

Take it

The walk stopped after 200 commits: lines older than c6e6792 point at it.

Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77

lines 1–2why, one step back
1.. _tutorial-views:
2

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 3–13why, one step back
3Step 5: The View Functions
4==========================56Now that the database connections are working we can start writing the7view functions. We will need four of them:89Show Entries10------------1112This view shows all the entries stored in the database. It listens on the13root of the application and will select title and text from the database.

Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530

lines 14–18why, one step back
14The one with the highest id (the newest entry) will be on top. The rows
15returned from the cursor are tuples with the columns ordered like specified16in the select statement. This is good enough for small applications like17here, but you might want to convert them into a dict. If you are18interested in how to do that, check out the :ref:`easy-querying` example.

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 19–50why, one step back
19
20The view function will pass the entries as dicts to the21`show_entries.html` template and return the rendered one::2223 @app.route('/')24 def show_entries():25 cur = g.db.execute('select title, text from entries order by id desc')26 entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]27 return render_template('show_entries.html', entries=entries)2829Add New Entry30-------------3132This view lets the user add new entries if he's logged in. This only33responds to `POST` requests, the actual form is shown on the34`show_entries` page. If everything worked out well we will35:func:`~flask.flash` an information message to the next request and36redirect back to the `show_entries` page::3738 @app.route('/add', methods=['POST'])39 def add_entry():40 if not session.get('logged_in'):41 abort(401)42 g.db.execute('insert into entries (title, text) values (?, ?)',43 [request.form['title'], request.form['text']])44 g.db.commit()45 flash('New entry was successfully posted')46 return redirect(url_for('show_entries'))4748Note that we check that the user is logged in here (the `logged_in` key is49present in the session and `True`).50

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoptione20534d

lines 51–57why, one step back
51.. admonition:: Security Note
5253 Be sure to use question marks when building SQL statements, as done in the54 example above. Otherwise, your app will be vulnerable to SQL injection when55 you use string formatting to build SQL statements.56 See :ref:`sqlite3` for more.57

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 58–62why, one step back
58Login and Logout
59----------------6061These functions are used to sign the user in and out. Login checks the62username and password against the ones from the configuration and sets the

Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530

lines 63–67why, one step back
63`logged_in` key in the session. If the user logged in successfully, that
64key is set to `True`, and the user is redirected back to the `show_entries`65page. In addition, a message is flashed that informs the user that he or66she was logged in successfully. If an error occurred, the template is67notified about that, and the user is asked again::

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 68–72why, one step back
68
69 @app.route('/login', methods=['GET', 'POST'])70 def login():71 error = None72 if request.method == 'POST':

Ported examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86

line 73why, one step back
73 if request.form['username'] != app.config['USERNAME']:

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

line 74why, one step back
74 error = 'Invalid username'

Ported examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86

line 75why, one step back
75 elif request.form['password'] != app.config['PASSWORD']:

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 76–82why, one step back
76 error = 'Invalid password'
77 else:78 session['logged_in'] = True79 flash('You were logged in')80 return redirect(url_for('show_entries'))81 return render_template('login.html', error=error)82

Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530

line 83why, one step back
83The logout function, on the other hand, removes that key from the session

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

line 84why, one step back
84again. We use a neat trick here: if you use the :meth:`~dict.pop` method

Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530

line 85why, one step back
85of the dict and pass a second parameter to it (the default), the method

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

line 86why, one step back
86will delete the key from the dictionary if present or do nothing when that

Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530

lines 87–88why, one step back
87key is not in there. This is helpful because now we don't have to check
88if the user was logged in.

Added MANIFEST.in. This fixes #39Armin Ronacherbefore adoptionc6e6792

lines 89–96why, one step back
89
90::9192 @app.route('/logout')93 def logout():94 session.pop('logged_in', None)95 flash('You were logged out')96 return redirect(url_for('show_entries'))

Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77

lines 97–98why, one step back
97
98Continue with :ref:`tutorial-templates`.