diff options
Diffstat (limited to 'app/static/doc/flask-docs/tutorial')
-rw-r--r-- | app/static/doc/flask-docs/tutorial/css.html | 132 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/dbcon.html | 154 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/dbinit.html | 166 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/folders.html | 122 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/index.html | 137 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/introduction.html | 128 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/schema.html | 125 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/setup.html | 183 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/templates.html | 212 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/testing.html | 113 | ||||
-rw-r--r-- | app/static/doc/flask-docs/tutorial/views.html | 206 |
11 files changed, 1678 insertions, 0 deletions
diff --git a/app/static/doc/flask-docs/tutorial/css.html b/app/static/doc/flask-docs/tutorial/css.html new file mode 100644 index 0000000..e226e94 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/css.html @@ -0,0 +1,132 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 7: Adding Style — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Bonus: Testing the Application" href="testing.html" /> + <link rel="prev" title="Step 6: The Templates" href="templates.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="testing.html" title="Bonus: Testing the Application" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="templates.html" title="Step 6: The Templates" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-7-adding-style"> +<span id="tutorial-css"></span><h1>Step 7: Adding Style<a class="headerlink" href="#step-7-adding-style" title="Permalink to this headline">¶</a></h1> +<p>Now that everything else works, it’s time to add some style to the +application. Just create a stylesheet called <cite>style.css</cite> in the <cite>static</cite> +folder we created before:</p> +<div class="highlight-css"><div class="highlight"><pre><span class="nt">body</span> <span class="p">{</span> <span class="k">font-family</span><span class="o">:</span> <span class="k">sans-serif</span><span class="p">;</span> <span class="k">background</span><span class="o">:</span> <span class="m">#eee</span><span class="p">;</span> <span class="p">}</span> +<span class="nt">a</span><span class="o">,</span> <span class="nt">h1</span><span class="o">,</span> <span class="nt">h2</span> <span class="p">{</span> <span class="k">color</span><span class="o">:</span> <span class="m">#377BA8</span><span class="p">;</span> <span class="p">}</span> +<span class="nt">h1</span><span class="o">,</span> <span class="nt">h2</span> <span class="p">{</span> <span class="k">font-family</span><span class="o">:</span> <span class="s1">'Georgia'</span><span class="o">,</span> <span class="k">serif</span><span class="p">;</span> <span class="k">margin</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="p">}</span> +<span class="nt">h1</span> <span class="p">{</span> <span class="k">border-bottom</span><span class="o">:</span> <span class="m">2px</span> <span class="k">solid</span> <span class="m">#eee</span><span class="p">;</span> <span class="p">}</span> +<span class="nt">h2</span> <span class="p">{</span> <span class="k">font-size</span><span class="o">:</span> <span class="m">1.2em</span><span class="p">;</span> <span class="p">}</span> + +<span class="nc">.page</span> <span class="p">{</span> <span class="k">margin</span><span class="o">:</span> <span class="m">2em</span> <span class="k">auto</span><span class="p">;</span> <span class="k">width</span><span class="o">:</span> <span class="m">35em</span><span class="p">;</span> <span class="k">border</span><span class="o">:</span> <span class="m">5px</span> <span class="k">solid</span> <span class="m">#ccc</span><span class="p">;</span> + <span class="k">padding</span><span class="o">:</span> <span class="m">0.8em</span><span class="p">;</span> <span class="k">background</span><span class="o">:</span> <span class="nb">white</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.entries</span> <span class="p">{</span> <span class="k">list-style</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span> <span class="k">margin</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.entries</span> <span class="nt">li</span> <span class="p">{</span> <span class="k">margin</span><span class="o">:</span> <span class="m">0.8em</span> <span class="m">1.2em</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.entries</span> <span class="nt">li</span> <span class="nt">h2</span> <span class="p">{</span> <span class="k">margin-left</span><span class="o">:</span> <span class="m">-1em</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.add-entry</span> <span class="p">{</span> <span class="k">font-size</span><span class="o">:</span> <span class="m">0.9em</span><span class="p">;</span> <span class="k">border-bottom</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#ccc</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.add-entry</span> <span class="nt">dl</span> <span class="p">{</span> <span class="k">font-weight</span><span class="o">:</span> <span class="k">bold</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.metanav</span> <span class="p">{</span> <span class="k">text-align</span><span class="o">:</span> <span class="k">right</span><span class="p">;</span> <span class="k">font-size</span><span class="o">:</span> <span class="m">0.8em</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span> <span class="m">0.3em</span><span class="p">;</span> + <span class="k">margin-bottom</span><span class="o">:</span> <span class="m">1em</span><span class="p">;</span> <span class="k">background</span><span class="o">:</span> <span class="m">#fafafa</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.flash</span> <span class="p">{</span> <span class="k">background</span><span class="o">:</span> <span class="m">#CEE5F5</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span> <span class="m">0.5em</span><span class="p">;</span> + <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#AACBE2</span><span class="p">;</span> <span class="p">}</span> +<span class="nc">.error</span> <span class="p">{</span> <span class="k">background</span><span class="o">:</span> <span class="m">#F0D6D6</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span> <span class="m">0.5em</span><span class="p">;</span> <span class="p">}</span> +</pre></div> +</div> +<p>Continue with <a class="reference internal" href="testing.html#tutorial-testing"><em>Bonus: Testing the Application</em></a>.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="templates.html" title="previous chapter">Step 6: The Templates</a></li> + <li>Next: <a href="testing.html" title="next chapter">Bonus: Testing the Application</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/css.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/dbcon.html b/app/static/doc/flask-docs/tutorial/dbcon.html new file mode 100644 index 0000000..91ed599 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/dbcon.html @@ -0,0 +1,154 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 4: Request Database Connections — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 5: The View Functions" href="views.html" /> + <link rel="prev" title="Step 3: Creating The Database" href="dbinit.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="views.html" title="Step 5: The View Functions" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="dbinit.html" title="Step 3: Creating The Database" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-4-request-database-connections"> +<span id="tutorial-dbcon"></span><h1>Step 4: Request Database Connections<a class="headerlink" href="#step-4-request-database-connections" title="Permalink to this headline">¶</a></h1> +<p>Now we know how we can open database connections and use them for scripts, +but how can we elegantly do that for requests? We will need the database +connection in all our functions so it makes sense to initialize them +before each request and shut them down afterwards.</p> +<p>Flask allows us to do that with the <a class="reference internal" href="../api.html#flask.Flask.before_request" title="flask.Flask.before_request"><tt class="xref py py-meth docutils literal"><span class="pre">before_request()</span></tt></a>, +<a class="reference internal" href="../api.html#flask.Flask.after_request" title="flask.Flask.after_request"><tt class="xref py py-meth docutils literal"><span class="pre">after_request()</span></tt></a> and <a class="reference internal" href="../api.html#flask.Flask.teardown_request" title="flask.Flask.teardown_request"><tt class="xref py py-meth docutils literal"><span class="pre">teardown_request()</span></tt></a> +decorators:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.before_request</span> +<span class="k">def</span> <span class="nf">before_request</span><span class="p">():</span> + <span class="n">g</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">connect_db</span><span class="p">()</span> + +<span class="nd">@app.teardown_request</span> +<span class="k">def</span> <span class="nf">teardown_request</span><span class="p">(</span><span class="n">exception</span><span class="p">):</span> + <span class="n">g</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> +</pre></div> +</div> +<p>Functions marked with <a class="reference internal" href="../api.html#flask.Flask.before_request" title="flask.Flask.before_request"><tt class="xref py py-meth docutils literal"><span class="pre">before_request()</span></tt></a> are called before +a request and passed no arguments. Functions marked with +<a class="reference internal" href="../api.html#flask.Flask.after_request" title="flask.Flask.after_request"><tt class="xref py py-meth docutils literal"><span class="pre">after_request()</span></tt></a> are called after a request and +passed the response that will be sent to the client. They have to return +that response object or a different one. They are however not guaranteed +to be executed if an exception is raised, this is where functions marked with +<a class="reference internal" href="../api.html#flask.Flask.teardown_request" title="flask.Flask.teardown_request"><tt class="xref py py-meth docutils literal"><span class="pre">teardown_request()</span></tt></a> come in. They get called after the +response has been constructed. They are not allowed to modify the request, and +their return values are ignored. If an exception occurred while the request was +being processed, it is passed to each function; otherwise, <cite>None</cite> is passed in.</p> +<p>We store our current database connection on the special <a class="reference internal" href="../api.html#flask.g" title="flask.g"><tt class="xref py py-data docutils literal"><span class="pre">g</span></tt></a> +object that Flask provides for us. This object stores information for one +request only and is available from within each function. Never store such +things on other objects because this would not work with threaded +environments. That special <a class="reference internal" href="../api.html#flask.g" title="flask.g"><tt class="xref py py-data docutils literal"><span class="pre">g</span></tt></a> object does some magic behind +the scenes to ensure it does the right thing.</p> +<p>Continue to <a class="reference internal" href="views.html#tutorial-views"><em>Step 5: The View Functions</em></a>.</p> +<div class="admonition hint"> +<p class="first admonition-title">Hint</p> +<p>Where do I put this code?</p> +<p>If you’ve been following along in this tutorial, you might be wondering +where to put the code from this step and the next. A logical place is to +group these module-level functions together, and put your new +<tt class="docutils literal"><span class="pre">before_request</span></tt> and <tt class="docutils literal"><span class="pre">teardown_request</span></tt> functions below your existing +<tt class="docutils literal"><span class="pre">init_db</span></tt> function (following the tutorial line-by-line).</p> +<p class="last">If you need a moment to find your bearings, take a look at how the <a class="reference external" href="http://github.com/mitsuhiko/flask/tree/master/examples/flaskr/">example +source</a> is organized. In Flask, you can put all of your application code +into a single Python module. You don’t have to, and if your app <a class="reference internal" href="../patterns/packages.html#larger-applications"><em>grows +larger</em></a>, it’s a good idea not to.</p> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="dbinit.html" title="previous chapter">Step 3: Creating The Database</a></li> + <li>Next: <a href="views.html" title="next chapter">Step 5: The View Functions</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/dbcon.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/dbinit.html b/app/static/doc/flask-docs/tutorial/dbinit.html new file mode 100644 index 0000000..413914e --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/dbinit.html @@ -0,0 +1,166 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 3: Creating The Database — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 4: Request Database Connections" href="dbcon.html" /> + <link rel="prev" title="Step 2: Application Setup Code" href="setup.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="dbcon.html" title="Step 4: Request Database Connections" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="setup.html" title="Step 2: Application Setup Code" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-3-creating-the-database"> +<span id="tutorial-dbinit"></span><h1>Step 3: Creating The Database<a class="headerlink" href="#step-3-creating-the-database" title="Permalink to this headline">¶</a></h1> +<p>Flaskr is a database powered application as outlined earlier, and more +precisely, an application powered by a relational database system. Such +systems need a schema that tells them how to store that information. So +before starting the server for the first time it’s important to create +that schema.</p> +<p>Such a schema can be created by piping the <cite>schema.sql</cite> file into the +<cite>sqlite3</cite> command as follows:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="n">sqlite3</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">flaskr</span><span class="o">.</span><span class="n">db</span> <span class="o"><</span> <span class="n">schema</span><span class="o">.</span><span class="n">sql</span> +</pre></div> +</div> +<p>The downside of this is that it requires the sqlite3 command to be +installed which is not necessarily the case on every system. Also one has +to provide the path to the database there which leaves some place for +errors. It’s a good idea to add a function that initializes the database +for you to the application.</p> +<p>If you want to do that, you first have to import the +<a class="reference external" href="http://docs.python.org/dev/library/contextlib.html#contextlib.closing" title="(in Python v3.3)"><tt class="xref py py-func docutils literal"><span class="pre">contextlib.closing()</span></tt></a> function from the contextlib package. If you +want to use Python 2.5 it’s also necessary to enable the <cite>with</cite> statement +first (<cite>__future__</cite> imports must be the very first import):</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">with_statement</span> +<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">closing</span> +</pre></div> +</div> +<p>Next we can create a function called <cite>init_db</cite> that initializes the +database. For this we can use the <cite>connect_db</cite> function we defined +earlier. Just add that function below the <cite>connect_db</cite> function:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">init_db</span><span class="p">():</span> + <span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">connect_db</span><span class="p">())</span> <span class="k">as</span> <span class="n">db</span><span class="p">:</span> + <span class="k">with</span> <span class="n">app</span><span class="o">.</span><span class="n">open_resource</span><span class="p">(</span><span class="s">'schema.sql'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> + <span class="n">db</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span><span class="o">.</span><span class="n">executescript</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span> + <span class="n">db</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> +</pre></div> +</div> +<p>The <a class="reference external" href="http://docs.python.org/dev/library/contextlib.html#contextlib.closing" title="(in Python v3.3)"><tt class="xref py py-func docutils literal"><span class="pre">closing()</span></tt></a> helper function allows us to keep a +connection open for the duration of the <cite>with</cite> block. The +<a class="reference internal" href="../api.html#flask.Flask.open_resource" title="flask.Flask.open_resource"><tt class="xref py py-func docutils literal"><span class="pre">open_resource()</span></tt></a> method of the application object +supports that functionality out of the box, so it can be used in the +<cite>with</cite> block directly. This function opens a file from the resource +location (your <cite>flaskr</cite> folder) and allows you to read from it. We are +using this here to execute a script on the database connection.</p> +<p>When we connect to a database we get a connection object (here called +<cite>db</cite>) that can give us a cursor. On that cursor there is a method to +execute a complete script. Finally we only have to commit the changes. +SQLite 3 and other transactional databases will not commit unless you +explicitly tell it to.</p> +<p>Now it is possible to create a database by starting up a Python shell and +importing and calling that function:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">flaskr</span> <span class="kn">import</span> <span class="n">init_db</span> +<span class="gp">>>> </span><span class="n">init_db</span><span class="p">()</span> +</pre></div> +</div> +<div class="admonition-troubleshooting admonition "> +<p class="first admonition-title">Troubleshooting</p> +<p class="last">If you get an exception later that a table cannot be found check that +you did call the <cite>init_db</cite> function and that your table names are +correct (singular vs. plural for example).</p> +</div> +<p>Continue with <a class="reference internal" href="dbcon.html#tutorial-dbcon"><em>Step 4: Request Database Connections</em></a></p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="setup.html" title="previous chapter">Step 2: Application Setup Code</a></li> + <li>Next: <a href="dbcon.html" title="next chapter">Step 4: Request Database Connections</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/dbinit.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/folders.html b/app/static/doc/flask-docs/tutorial/folders.html new file mode 100644 index 0000000..a711ab9 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/folders.html @@ -0,0 +1,122 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 0: Creating The Folders — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 1: Database Schema" href="schema.html" /> + <link rel="prev" title="Introducing Flaskr" href="introduction.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="schema.html" title="Step 1: Database Schema" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="introduction.html" title="Introducing Flaskr" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-0-creating-the-folders"> +<span id="tutorial-folders"></span><h1>Step 0: Creating The Folders<a class="headerlink" href="#step-0-creating-the-folders" title="Permalink to this headline">¶</a></h1> +<p>Before we get started, let’s create the folders needed for this +application:</p> +<div class="highlight-python"><pre>/flaskr + /static + /templates</pre> +</div> +<p>The <cite>flaskr</cite> folder is not a python package, but just something where we +drop our files. Directly into this folder we will then put our database +schema as well as main module in the following steps. The files inside +the <cite>static</cite> folder are available to users of the application via <cite>HTTP</cite>. +This is the place where css and javascript files go. Inside the +<cite>templates</cite> folder Flask will look for <a class="reference external" href="http://jinja.pocoo.org/2/">Jinja2</a> templates. The +templates you create later in the tutorial will go in this directory.</p> +<p>Continue with <a class="reference internal" href="schema.html#tutorial-schema"><em>Step 1: Database Schema</em></a>.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="introduction.html" title="previous chapter">Introducing Flaskr</a></li> + <li>Next: <a href="schema.html" title="next chapter">Step 1: Database Schema</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/folders.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/index.html b/app/static/doc/flask-docs/tutorial/index.html new file mode 100644 index 0000000..cc010cb --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/index.html @@ -0,0 +1,137 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Tutorial — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="next" title="Introducing Flaskr" href="introduction.html" /> + <link rel="prev" title="Quickstart" href="../quickstart.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="introduction.html" title="Introducing Flaskr" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="../quickstart.html" title="Quickstart" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="tutorial"> +<span id="id1"></span><h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1> +<p>You want to develop an application with Python and Flask? Here you have +the chance to learn that by example. In this tutorial we will create a +simple microblog application. It only supports one user that can create +text-only entries and there are no feeds or comments, but it still +features everything you need to get started. We will use Flask and SQLite +as database which comes out of the box with Python, so there is nothing +else you need.</p> +<p>If you want the full sourcecode in advance or for comparison, check out +the <a class="reference external" href="http://github.com/mitsuhiko/flask/tree/master/examples/flaskr/">example source</a>.</p> +<div class="toctree-wrapper compound"> +<ul> +<li class="toctree-l1"><a class="reference internal" href="introduction.html">Introducing Flaskr</a></li> +<li class="toctree-l1"><a class="reference internal" href="folders.html">Step 0: Creating The Folders</a></li> +<li class="toctree-l1"><a class="reference internal" href="schema.html">Step 1: Database Schema</a></li> +<li class="toctree-l1"><a class="reference internal" href="setup.html">Step 2: Application Setup Code</a></li> +<li class="toctree-l1"><a class="reference internal" href="dbinit.html">Step 3: Creating The Database</a></li> +<li class="toctree-l1"><a class="reference internal" href="dbcon.html">Step 4: Request Database Connections</a></li> +<li class="toctree-l1"><a class="reference internal" href="views.html">Step 5: The View Functions</a><ul> +<li class="toctree-l2"><a class="reference internal" href="views.html#show-entries">Show Entries</a></li> +<li class="toctree-l2"><a class="reference internal" href="views.html#add-new-entry">Add New Entry</a></li> +<li class="toctree-l2"><a class="reference internal" href="views.html#login-and-logout">Login and Logout</a></li> +</ul> +</li> +<li class="toctree-l1"><a class="reference internal" href="templates.html">Step 6: The Templates</a><ul> +<li class="toctree-l2"><a class="reference internal" href="templates.html#layout-html">layout.html</a></li> +<li class="toctree-l2"><a class="reference internal" href="templates.html#show-entries-html">show_entries.html</a></li> +<li class="toctree-l2"><a class="reference internal" href="templates.html#login-html">login.html</a></li> +</ul> +</li> +<li class="toctree-l1"><a class="reference internal" href="css.html">Step 7: Adding Style</a></li> +<li class="toctree-l1"><a class="reference internal" href="testing.html">Bonus: Testing the Application</a></li> +</ul> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li>Previous: <a href="../quickstart.html" title="previous chapter">Quickstart</a></li> + <li>Next: <a href="introduction.html" title="next chapter">Introducing Flaskr</a></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/index.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/introduction.html b/app/static/doc/flask-docs/tutorial/introduction.html new file mode 100644 index 0000000..3598713 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/introduction.html @@ -0,0 +1,128 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Introducing Flaskr — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 0: Creating The Folders" href="folders.html" /> + <link rel="prev" title="Tutorial" href="index.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="folders.html" title="Step 0: Creating The Folders" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="index.html" title="Tutorial" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="introducing-flaskr"> +<span id="tutorial-introduction"></span><h1>Introducing Flaskr<a class="headerlink" href="#introducing-flaskr" title="Permalink to this headline">¶</a></h1> +<p>We will call our blogging application flaskr here, feel free to chose a +less web-2.0-ish name ;) Basically we want it to do the following things:</p> +<ol class="arabic simple"> +<li>let the user sign in and out with credentials specified in the +configuration. Only one user is supported.</li> +<li>when the user is logged in they can add new entries to the page +consisting of a text-only title and some HTML for the text. This HTML +is not sanitized because we trust the user here.</li> +<li>the page shows all entries so far in reverse order (newest on top) and +the user can add new ones from there if logged in.</li> +</ol> +<p>We will be using SQLite3 directly for that application because it’s good +enough for an application of that size. For larger applications however +it makes a lot of sense to use <a class="reference external" href="http://www.sqlalchemy.org/">SQLAlchemy</a> that handles database +connections in a more intelligent way, allows you to target different +relational databases at once and more. You might also want to consider +one of the popular NoSQL databases if your data is more suited for those.</p> +<p>Here a screenshot from the final application:</p> +<img alt="screenshot of the final application" class="screenshot align-center" src="../_images/flaskr.png" /> +<p>Continue with <a class="reference internal" href="folders.html#tutorial-folders"><em>Step 0: Creating The Folders</em></a>.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="index.html" title="previous chapter">Tutorial</a></li> + <li>Next: <a href="folders.html" title="next chapter">Step 0: Creating The Folders</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/introduction.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/schema.html b/app/static/doc/flask-docs/tutorial/schema.html new file mode 100644 index 0000000..9794054 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/schema.html @@ -0,0 +1,125 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 1: Database Schema — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 2: Application Setup Code" href="setup.html" /> + <link rel="prev" title="Step 0: Creating The Folders" href="folders.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="setup.html" title="Step 2: Application Setup Code" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="folders.html" title="Step 0: Creating The Folders" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-1-database-schema"> +<span id="tutorial-schema"></span><h1>Step 1: Database Schema<a class="headerlink" href="#step-1-database-schema" title="Permalink to this headline">¶</a></h1> +<p>First we want to create the database schema. For this application only a +single table is needed and we only want to support SQLite so that is quite +easy. Just put the following contents into a file named <cite>schema.sql</cite> in +the just created <cite>flaskr</cite> folder:</p> +<div class="highlight-sql"><div class="highlight"><pre><span class="k">drop</span> <span class="k">table</span> <span class="n">if</span> <span class="k">exists</span> <span class="n">entries</span><span class="p">;</span> +<span class="k">create</span> <span class="k">table</span> <span class="n">entries</span> <span class="p">(</span> + <span class="n">id</span> <span class="nb">integer</span> <span class="k">primary</span> <span class="k">key</span> <span class="n">autoincrement</span><span class="p">,</span> + <span class="n">title</span> <span class="n">string</span> <span class="k">not</span> <span class="k">null</span><span class="p">,</span> + <span class="nb">text</span> <span class="n">string</span> <span class="k">not</span> <span class="k">null</span> +<span class="p">);</span> +</pre></div> +</div> +<p>This schema consists of a single table called <cite>entries</cite> and each row in +this table has an <cite>id</cite>, a <cite>title</cite> and a <cite>text</cite>. The <cite>id</cite> is an +automatically incrementing integer and a primary key, the other two are +strings that must not be null.</p> +<p>Continue with <a class="reference internal" href="setup.html#tutorial-setup"><em>Step 2: Application Setup Code</em></a>.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="folders.html" title="previous chapter">Step 0: Creating The Folders</a></li> + <li>Next: <a href="setup.html" title="next chapter">Step 2: Application Setup Code</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/schema.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/setup.html b/app/static/doc/flask-docs/tutorial/setup.html new file mode 100644 index 0000000..142281d --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/setup.html @@ -0,0 +1,183 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 2: Application Setup Code — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 3: Creating The Database" href="dbinit.html" /> + <link rel="prev" title="Step 1: Database Schema" href="schema.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="dbinit.html" title="Step 3: Creating The Database" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="schema.html" title="Step 1: Database Schema" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-2-application-setup-code"> +<span id="tutorial-setup"></span><h1>Step 2: Application Setup Code<a class="headerlink" href="#step-2-application-setup-code" title="Permalink to this headline">¶</a></h1> +<p>Now that we have the schema in place we can create the application module. +Let’s call it <cite>flaskr.py</cite> inside the <cite>flaskr</cite> folder. For starters we +will add the imports we will need as well as the config section. For +small applications it’s a possibility to drop the configuration directly +into the module which we will be doing here. However a cleaner solution +would be to create a separate <cite>.ini</cite> or <cite>.py</cite> file and load that or import +the values from there.</p> +<div class="highlight-python"><div class="highlight"><pre><span class="c"># all the imports</span> +<span class="kn">import</span> <span class="nn">sqlite3</span> +<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">redirect</span><span class="p">,</span> <span class="n">url_for</span><span class="p">,</span> \ + <span class="n">abort</span><span class="p">,</span> <span class="n">render_template</span><span class="p">,</span> <span class="n">flash</span> + +<span class="c"># configuration</span> +<span class="n">DATABASE</span> <span class="o">=</span> <span class="s">'/tmp/flaskr.db'</span> +<span class="n">DEBUG</span> <span class="o">=</span> <span class="bp">True</span> +<span class="n">SECRET_KEY</span> <span class="o">=</span> <span class="s">'development key'</span> +<span class="n">USERNAME</span> <span class="o">=</span> <span class="s">'admin'</span> +<span class="n">PASSWORD</span> <span class="o">=</span> <span class="s">'default'</span> +</pre></div> +</div> +<p>Next we can create our actual application and initialize it with the +config from the same file:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="c"># create our little application :)</span> +<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> +<span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">from_object</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> +</pre></div> +</div> +<p><a class="reference internal" href="../api.html#flask.Config.from_object" title="flask.Config.from_object"><tt class="xref py py-meth docutils literal"><span class="pre">from_object()</span></tt></a> will look at the given object (if it’s a +string it will import it) and then look for all uppercase variables +defined there. In our case, the configuration we just wrote a few lines +of code above. You can also move that into a separate file.</p> +<p>It is also a good idea to be able to load a configuration from a +configurable file. This is what <a class="reference internal" href="../api.html#flask.Config.from_envvar" title="flask.Config.from_envvar"><tt class="xref py py-meth docutils literal"><span class="pre">from_envvar()</span></tt></a> can +do:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">from_envvar</span><span class="p">(</span><span class="s">'FLASKR_SETTINGS'</span><span class="p">,</span> <span class="n">silent</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> +</pre></div> +</div> +<p>That way someone can set an environment variable called +<span class="target" id="index-0"></span><tt class="xref std std-envvar docutils literal"><span class="pre">FLASKR_SETTINGS</span></tt> to specify a config file to be loaded which will +then override the default values. The silent switch just tells Flask to +not complain if no such environment key is set.</p> +<p>The <cite>secret_key</cite> is needed to keep the client-side sessions secure. +Choose that key wisely and as hard to guess and complex as possible. The +debug flag enables or disables the interactive debugger. Never leave +debug mode activated in a production system because it will allow users to +execute code on the server!</p> +<p>We also add a method to easily connect to the database specified. That +can be used to open a connection on request and also from the interactive +Python shell or a script. This will come in handy later.</p> +<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">connect_db</span><span class="p">():</span> + <span class="k">return</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'DATABASE'</span><span class="p">])</span> +</pre></div> +</div> +<p>Finally we just add a line to the bottom of the file that fires up the +server if we want to run that file as a standalone application:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> + <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> +</pre></div> +</div> +<p>With that out of the way you should be able to start up the application +without problems. Do this with the following command:</p> +<div class="highlight-python"><pre>python flaskr.py</pre> +</div> +<p>You will see a message telling you that server has started along with +the address at which you can access it.</p> +<p>When you head over to the server in your browser you will get an 404 +page not found error because we don’t have any views yet. But we will +focus on that a little later. First we should get the database working.</p> +<div class="admonition-externally-visible-server admonition "> +<p class="first admonition-title">Externally Visible Server</p> +<p class="last">Want your server to be publicly available? Check out the +<a class="reference internal" href="../quickstart.html#public-server"><em>externally visible server</em></a> section for more +information.</p> +</div> +<p>Continue with <a class="reference internal" href="dbinit.html#tutorial-dbinit"><em>Step 3: Creating The Database</em></a>.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="schema.html" title="previous chapter">Step 1: Database Schema</a></li> + <li>Next: <a href="dbinit.html" title="next chapter">Step 3: Creating The Database</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/setup.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/templates.html b/app/static/doc/flask-docs/tutorial/templates.html new file mode 100644 index 0000000..9c99a35 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/templates.html @@ -0,0 +1,212 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 6: The Templates — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 7: Adding Style" href="css.html" /> + <link rel="prev" title="Step 5: The View Functions" href="views.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="css.html" title="Step 7: Adding Style" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="views.html" title="Step 5: The View Functions" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-6-the-templates"> +<span id="tutorial-templates"></span><h1>Step 6: The Templates<a class="headerlink" href="#step-6-the-templates" title="Permalink to this headline">¶</a></h1> +<p>Now we should start working on the templates. If we request the URLs now +we would only get an exception that Flask cannot find the templates. The +templates are using <a class="reference external" href="http://jinja.pocoo.org/2/documentation/templates">Jinja2</a> syntax and have autoescaping enabled by +default. This means that unless you mark a value in the code with +<a class="reference internal" href="../api.html#flask.Markup" title="flask.Markup"><tt class="xref py py-class docutils literal"><span class="pre">Markup</span></tt></a> or with the <tt class="docutils literal"><span class="pre">|safe</span></tt> filter in the template, +Jinja2 will ensure that special characters such as <tt class="docutils literal"><span class="pre"><</span></tt> or <tt class="docutils literal"><span class="pre">></span></tt> are +escaped with their XML equivalents.</p> +<p>We are also using template inheritance which makes it possible to reuse +the layout of the website in all pages.</p> +<p>Put the following templates into the <cite>templates</cite> folder:</p> +<div class="section" id="layout-html"> +<h2>layout.html<a class="headerlink" href="#layout-html" title="Permalink to this headline">¶</a></h2> +<p>This template contains the HTML skeleton, the header and a link to log in +(or log out if the user was already logged in). It also displays the +flashed messages if there are any. The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">body</span> <span class="pre">%}</span></tt> block can be +replaced by a block of the same name (<tt class="docutils literal"><span class="pre">body</span></tt>) in a child template.</p> +<p>The <a class="reference internal" href="../api.html#flask.session" title="flask.session"><tt class="xref py py-class docutils literal"><span class="pre">session</span></tt></a> dict is available in the template as well and +you can use that to check if the user is logged in or not. Note that in +Jinja you can access missing attributes and items of objects / dicts which +makes the following code work, even if there is no <tt class="docutils literal"><span class="pre">'logged_in'</span></tt> key in +the session:</p> +<div class="highlight-html+jinja"><div class="highlight"><pre><span class="cp"><!doctype html></span> +<span class="nt"><title></span>Flaskr<span class="nt"></title></span> +<span class="nt"><link</span> <span class="na">rel=</span><span class="s">stylesheet</span> <span class="na">type=</span><span class="s">text/css</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'static'</span><span class="o">,</span> <span class="nv">filename</span><span class="o">=</span><span class="s1">'style.css'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span> +<span class="nt"><div</span> <span class="na">class=</span><span class="s">page</span><span class="nt">></span> + <span class="nt"><h1></span>Flaskr<span class="nt"></h1></span> + <span class="nt"><div</span> <span class="na">class=</span><span class="s">metanav</span><span class="nt">></span> + <span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">session.logged_in</span> <span class="cp">%}</span> + <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'login'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>log in<span class="nt"></a></span> + <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span> + <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'logout'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>log out<span class="nt"></a></span> + <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> + <span class="nt"></div></span> + <span class="cp">{%</span> <span class="k">for</span> <span class="nv">message</span> <span class="k">in</span> <span class="nv">get_flashed_messages</span><span class="o">()</span> <span class="cp">%}</span> + <span class="nt"><div</span> <span class="na">class=</span><span class="s">flash</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">message</span> <span class="cp">}}</span><span class="nt"></div></span> + <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> + <span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span> +<span class="nt"></div></span> +</pre></div> +</div> +</div> +<div class="section" id="show-entries-html"> +<h2>show_entries.html<a class="headerlink" href="#show-entries-html" title="Permalink to this headline">¶</a></h2> +<p>This template extends the <cite>layout.html</cite> template from above to display the +messages. Note that the <cite>for</cite> loop iterates over the messages we passed +in with the <a class="reference internal" href="../api.html#flask.render_template" title="flask.render_template"><tt class="xref py py-func docutils literal"><span class="pre">render_template()</span></tt></a> function. We also tell the +form to submit to your <cite>add_entry</cite> function and use <cite>POST</cite> as <cite>HTTP</cite> +method:</p> +<div class="highlight-html+jinja"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"layout.html"</span> <span class="cp">%}</span> +<span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}</span> + <span class="cp">{%</span> <span class="k">if</span> <span class="nv">session.logged_in</span> <span class="cp">%}</span> + <span class="nt"><form</span> <span class="na">action=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'add_entry'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">method=</span><span class="s">post</span> <span class="na">class=</span><span class="s">add-entry</span><span class="nt">></span> + <span class="nt"><dl></span> + <span class="nt"><dt></span>Title: + <span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">text</span> <span class="na">size=</span><span class="s">30</span> <span class="na">name=</span><span class="s">title</span><span class="nt">></span> + <span class="nt"><dt></span>Text: + <span class="nt"><dd><textarea</span> <span class="na">name=</span><span class="s">text</span> <span class="na">rows=</span><span class="s">5</span> <span class="na">cols=</span><span class="s">40</span><span class="nt">></textarea></span> + <span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">Share</span><span class="nt">></span> + <span class="nt"></dl></span> + <span class="nt"></form></span> + <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> + <span class="nt"><ul</span> <span class="na">class=</span><span class="s">entries</span><span class="nt">></span> + <span class="cp">{%</span> <span class="k">for</span> <span class="nv">entry</span> <span class="k">in</span> <span class="nv">entries</span> <span class="cp">%}</span> + <span class="nt"><li><h2></span><span class="cp">{{</span> <span class="nv">entry.title</span> <span class="cp">}}</span><span class="nt"></h2></span><span class="cp">{{</span> <span class="nv">entry.text</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span> + <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span> + <span class="nt"><li><em></span>Unbelievable. No entries here so far<span class="nt"></em></span> + <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> + <span class="nt"></ul></span> +<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span> +</pre></div> +</div> +</div> +<div class="section" id="login-html"> +<h2>login.html<a class="headerlink" href="#login-html" title="Permalink to this headline">¶</a></h2> +<p>Finally the login template which basically just displays a form to allow +the user to login:</p> +<div class="highlight-html+jinja"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"layout.html"</span> <span class="cp">%}</span> +<span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}</span> + <span class="nt"><h2></span>Login<span class="nt"></h2></span> + <span class="cp">{%</span> <span class="k">if</span> <span class="nv">error</span> <span class="cp">%}</span><span class="nt"><p</span> <span class="na">class=</span><span class="s">error</span><span class="nt">><strong></span>Error:<span class="nt"></strong></span> <span class="cp">{{</span> <span class="nv">error</span> <span class="cp">}}{%</span> <span class="k">endif</span> <span class="cp">%}</span> + <span class="nt"><form</span> <span class="na">action=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'login'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">method=</span><span class="s">post</span><span class="nt">></span> + <span class="nt"><dl></span> + <span class="nt"><dt></span>Username: + <span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">text</span> <span class="na">name=</span><span class="s">username</span><span class="nt">></span> + <span class="nt"><dt></span>Password: + <span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">password</span> <span class="na">name=</span><span class="s">password</span><span class="nt">></span> + <span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">Login</span><span class="nt">></span> + <span class="nt"></dl></span> + <span class="nt"></form></span> +<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span> +</pre></div> +</div> +<p>Continue with <a class="reference internal" href="css.html#tutorial-css"><em>Step 7: Adding Style</em></a>.</p> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p> + <h3><a href="../index.html">Table Of Contents</a></h3> + <ul> +<li><a class="reference internal" href="#">Step 6: The Templates</a><ul> +<li><a class="reference internal" href="#layout-html">layout.html</a></li> +<li><a class="reference internal" href="#show-entries-html">show_entries.html</a></li> +<li><a class="reference internal" href="#login-html">login.html</a></li> +</ul> +</li> +</ul> +<h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="views.html" title="previous chapter">Step 5: The View Functions</a></li> + <li>Next: <a href="css.html" title="next chapter">Step 7: Adding Style</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/templates.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/testing.html b/app/static/doc/flask-docs/tutorial/testing.html new file mode 100644 index 0000000..ba82db6 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/testing.html @@ -0,0 +1,113 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Bonus: Testing the Application — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Templates" href="../templating.html" /> + <link rel="prev" title="Step 7: Adding Style" href="css.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="../templating.html" title="Templates" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="css.html" title="Step 7: Adding Style" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="bonus-testing-the-application"> +<span id="tutorial-testing"></span><h1>Bonus: Testing the Application<a class="headerlink" href="#bonus-testing-the-application" title="Permalink to this headline">¶</a></h1> +<p>Now that you have finished the application and everything works as +expected, it’s probably not a bad idea to add automated tests to simplify +modifications in the future. The application above is used as a basic +example of how to perform unittesting in the <a class="reference internal" href="../testing.html#testing"><em>Testing Flask Applications</em></a> section of the +documentation. Go there to see how easy it is to test Flask applications.</p> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p><h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="css.html" title="previous chapter">Step 7: Adding Style</a></li> + <li>Next: <a href="../templating.html" title="next chapter">Templates</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/testing.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file diff --git a/app/static/doc/flask-docs/tutorial/views.html b/app/static/doc/flask-docs/tutorial/views.html new file mode 100644 index 0000000..feed988 --- /dev/null +++ b/app/static/doc/flask-docs/tutorial/views.html @@ -0,0 +1,206 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Step 5: The View Functions — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Tutorial" href="index.html" /> + <link rel="next" title="Step 6: The Templates" href="templates.html" /> + <link rel="prev" title="Step 4: Request Database Connections" href="dbcon.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="templates.html" title="Step 6: The Templates" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="dbcon.html" title="Step 4: Request Database Connections" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Tutorial</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="step-5-the-view-functions"> +<span id="tutorial-views"></span><h1>Step 5: The View Functions<a class="headerlink" href="#step-5-the-view-functions" title="Permalink to this headline">¶</a></h1> +<p>Now that the database connections are working we can start writing the +view functions. We will need four of them:</p> +<div class="section" id="show-entries"> +<h2>Show Entries<a class="headerlink" href="#show-entries" title="Permalink to this headline">¶</a></h2> +<p>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 <a class="reference internal" href="../patterns/sqlite3.html#easy-querying"><em>Easy Querying</em></a> example.</p> +<p>The view function will pass the entries as dicts to the +<cite>show_entries.html</cite> template and return the rendered one:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span> +<span class="k">def</span> <span class="nf">show_entries</span><span class="p">():</span> + <span class="n">cur</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">'select title, text from entries order by id desc'</span><span class="p">)</span> + <span class="n">entries</span> <span class="o">=</span> <span class="p">[</span><span class="nb">dict</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">text</span><span class="o">=</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()]</span> + <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">'show_entries.html'</span><span class="p">,</span> <span class="n">entries</span><span class="o">=</span><span class="n">entries</span><span class="p">)</span> +</pre></div> +</div> +</div> +<div class="section" id="add-new-entry"> +<h2>Add New Entry<a class="headerlink" href="#add-new-entry" title="Permalink to this headline">¶</a></h2> +<p>This view lets the user add new entries if they are logged in. This only +responds to <cite>POST</cite> requests, the actual form is shown on the +<cite>show_entries</cite> page. If everything worked out well we will +<a class="reference internal" href="../api.html#flask.flash" title="flask.flash"><tt class="xref py py-func docutils literal"><span class="pre">flash()</span></tt></a> an information message to the next request and +redirect back to the <cite>show_entries</cite> page:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/add'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'POST'</span><span class="p">])</span> +<span class="k">def</span> <span class="nf">add_entry</span><span class="p">():</span> + <span class="k">if</span> <span class="ow">not</span> <span class="n">session</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'logged_in'</span><span class="p">):</span> + <span class="n">abort</span><span class="p">(</span><span class="mi">401</span><span class="p">)</span> + <span class="n">g</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">'insert into entries (title, text) values (?, ?)'</span><span class="p">,</span> + <span class="p">[</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'title'</span><span class="p">],</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'text'</span><span class="p">]])</span> + <span class="n">g</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> + <span class="n">flash</span><span class="p">(</span><span class="s">'New entry was successfully posted'</span><span class="p">)</span> + <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">'show_entries'</span><span class="p">))</span> +</pre></div> +</div> +<p>Note that we check that the user is logged in here (the <cite>logged_in</cite> key is +present in the session and <cite>True</cite>).</p> +<div class="admonition-security-note admonition "> +<p class="first admonition-title">Security Note</p> +<p class="last">Be sure to use question marks when building SQL statements, as done in the +example above. Otherwise, your app will be vulnerable to SQL injection when +you use string formatting to build SQL statements. +See <a class="reference internal" href="../patterns/sqlite3.html#sqlite3"><em>Using SQLite 3 with Flask</em></a> for more.</p> +</div> +</div> +<div class="section" id="login-and-logout"> +<h2>Login and Logout<a class="headerlink" href="#login-and-logout" title="Permalink to this headline">¶</a></h2> +<p>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 +<cite>logged_in</cite> key in the session. If the user logged in successfully, that +key is set to <cite>True</cite>, and the user is redirected back to the <cite>show_entries</cite> +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:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/login'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'GET'</span><span class="p">,</span> <span class="s">'POST'</span><span class="p">])</span> +<span class="k">def</span> <span class="nf">login</span><span class="p">():</span> + <span class="n">error</span> <span class="o">=</span> <span class="bp">None</span> + <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="p">:</span> + <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'username'</span><span class="p">]</span> <span class="o">!=</span> <span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'USERNAME'</span><span class="p">]:</span> + <span class="n">error</span> <span class="o">=</span> <span class="s">'Invalid username'</span> + <span class="k">elif</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'password'</span><span class="p">]</span> <span class="o">!=</span> <span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'PASSWORD'</span><span class="p">]:</span> + <span class="n">error</span> <span class="o">=</span> <span class="s">'Invalid password'</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">session</span><span class="p">[</span><span class="s">'logged_in'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span> + <span class="n">flash</span><span class="p">(</span><span class="s">'You were logged in'</span><span class="p">)</span> + <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">'show_entries'</span><span class="p">))</span> + <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">'login.html'</span><span class="p">,</span> <span class="n">error</span><span class="o">=</span><span class="n">error</span><span class="p">)</span> +</pre></div> +</div> +<p>The logout function, on the other hand, removes that key from the session +again. We use a neat trick here: if you use the <a class="reference external" href="http://docs.python.org/dev/library/stdtypes.html#dict.pop" title="(in Python v3.3)"><tt class="xref py py-meth docutils literal"><span class="pre">pop()</span></tt></a> 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.</p> +<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/logout'</span><span class="p">)</span> +<span class="k">def</span> <span class="nf">logout</span><span class="p">():</span> + <span class="n">session</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">'logged_in'</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> + <span class="n">flash</span><span class="p">(</span><span class="s">'You were logged out'</span><span class="p">)</span> + <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">'show_entries'</span><span class="p">))</span> +</pre></div> +</div> +<p>Continue with <a class="reference internal" href="templates.html#tutorial-templates"><em>Step 6: The Templates</em></a>.</p> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p> + <h3><a href="../index.html">Table Of Contents</a></h3> + <ul> +<li><a class="reference internal" href="#">Step 5: The View Functions</a><ul> +<li><a class="reference internal" href="#show-entries">Show Entries</a></li> +<li><a class="reference internal" href="#add-new-entry">Add New Entry</a></li> +<li><a class="reference internal" href="#login-and-logout">Login and Logout</a></li> +</ul> +</li> +</ul> +<h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Tutorial</a><ul> + <li>Previous: <a href="dbcon.html" title="previous chapter">Step 4: Request Database Connections</a></li> + <li>Next: <a href="templates.html" title="next chapter">Step 6: The Templates</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/tutorial/views.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file |