Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files


  • Loading branch information...
commit c1f174e4b9bbc9ef6c722869d87cee7ef525391f 1 parent dbf8c05
@gfreezy gfreezy authored
Showing with 34 additions and 40 deletions.
  1. +34 −40 docs/tutorial/views.rst
74 docs/tutorial/views.rst
@@ -1,24 +1,22 @@
.. _tutorial-views:
-Step 5: The View Functions
+第五步: 视图函数
-Now that the database connections are working we can start writing the
-view functions. We will need four of them:
-Show Entries
-This view shows all the entries stored in the database. It listens on the
-root of the application and will select title and text from the database.
-The one with the highest id (the newest entry) will be on top. The rows
-returned from the cursor are tuples with the columns ordered like specified
-in the select statement. This is good enough for small applications like
-here, but you might want to convert them into a dict. If you are
-interested in how to do that, check out the :ref:`easy-querying` example.
+:ref:`easy-querying` 的示例。
-The view function will pass the entries as dicts to the
-`show_entries.html` template and return the rendered one::
+视图函数会把所有的文章以字典的方式传送给 `show_entries.html`
def show_entries():
@@ -26,14 +24,13 @@ The view function will pass the entries as dicts to the
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries)
-Add New Entry
-This view lets the user add new entries if he's logged in. This only
-responds to `POST` requests, the actual form is shown on the
-`show_entries` page. If everything worked out well we will
-:func:`~flask.flash` an information message to the next request and
-redirect back to the `show_entries` page::
+这个视图用来让已登陆的用户发表新文章。它只对以 `POST`
+方式提交的请求回应,实际的表单显示在 `show_entries`
+页面上。如果一切都没有出问题的话,我们用 `~flask.flash`
+向下一次请求发送一条信息,然后重定向回 `show_entries` 页面::
@app.route('/add', methods=['POST'])
def add_entry():
@@ -45,8 +42,8 @@ redirect back to the `show_entries` page::
flash('New entry was successfully posted')
return redirect(url_for('show_entries'))
-Note that we check that the user is logged in here (the `logged_in` key is
-present in the session and `True`).
+注意,我们在这里检查了用户是否已经登陆( `logged_in`
+键在session中存在,而且值为 `True` )。
.. admonition:: Security Note
@@ -55,16 +52,14 @@ present in the session and `True`).
you use string formatting to build SQL statements.
See :ref:`sqlite3` for more.
-Login and Logout
-These functions are used to sign the user in and out. Login checks the
-username and password against the ones from the configuration and sets the
-`logged_in` key in the session. If the user logged in successfully, that
-key is set to `True`, and the user is redirected back to the `show_entries`
-page. In addition, a message is flashed that informs the user that he or
-she was logged in successfully. If an error occurred, the template is
-notified about that, and the user is asked again::
+中的数据进行比较,并相应的设置session中的 `logged_in`
+键。如果用户登陆成功,那么这个键会被设置成 `True` ,然后用户会被重定向到
+`show_entries` 页面。并且还会flash一条消息来提示用户登陆成功。如果登陆发生
@app.route('/login', methods=['GET', 'POST'])
def login():
@@ -80,12 +75,11 @@ notified about that, and the user is asked again::
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
-The logout function, on the other hand, removes that key from the session
-again. We use a neat trick here: if you use the :meth:`~dict.pop` method
-of the dict and pass a second parameter to it (the default), the method
-will delete the key from the dictionary if present or do nothing when that
-key is not in there. This is helpful because now we don't have to check
-if the user was logged in.
+注销函数所作的正好相反。它从session中删除 `logged_in` 键。我们在这里使用的
+一个简洁的小技巧:如果你在使用字典的 :meth:`~dict.pop` 方法时,给了它第二个
@@ -95,4 +89,4 @@ if the user was logged in.
flash('You were logged out')
return redirect(url_for('show_entries'))
-Continue with :ref:`tutorial-templates`.
+继续 :ref:`tutorial-templates`.
Please sign in to comment.
Something went wrong with that request. Please try again.