/usr/share/doc/python-pecan-doc/html/simple_forms_processing.html is in python-pecan-doc 1.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | <!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>Example Application: Simple Forms Processing — Pecan 1.2.1 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.2.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Example Application: Simple AJAX" href="simple_ajax.html" />
<link rel="prev" title="Custom Error Documents" href="errors.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="simple_ajax.html" title="Example Application: Simple AJAX"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="errors.html" title="Custom Error Documents"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Pecan 1.2.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="example-application-simple-forms-processing">
<span id="simple-forms-processing"></span><h1>Example Application: Simple Forms Processing<a class="headerlink" href="#example-application-simple-forms-processing" title="Permalink to this headline">¶</a></h1>
<p>This guide will walk you through building a simple Pecan web application that will do some simple forms processing.</p>
<div class="section" id="project-setup">
<h2>Project Setup<a class="headerlink" href="#project-setup" title="Permalink to this headline">¶</a></h2>
<p>First, you’ll need to install Pecan:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pip install pecan
</pre></div>
</div>
<p>Use Pecan’s basic template support to start a new project:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pecan create mywebsite
$ cd mywebsite
</pre></div>
</div>
<p>Install the new project in development mode:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python setup.py develop
</pre></div>
</div>
<p>With the project ready, go into the <code class="docutils literal"><span class="pre">templates</span></code> folder and edit the <code class="docutils literal"><span class="pre">index.html</span></code> file. Modify it so that it resembles this:</p>
<div class="highlight-html"><div class="highlight"><pre><span></span><%inherit file="layout.html" />
<%def name="title()">
Welcome to Pecan!
</%def>
<header>
<h1><img src="/images/logo.png" /></h1>
</header>
<div id="content">
<form method="POST" action="/">
<fieldset>
<p>Enter a message: <input name="message" /></p>
<p>Enter your first name: <input name="first_name" /></p>
<input type="submit" value="Submit" />
</fieldset>
</form>
% if not form_post_data is UNDEFINED:
<p>${form_post_data['first_name']}, your message is: ${form_post_data['message']}</p>
% endif
</div>
</pre></div>
</div>
<p><strong>What did we just do?</strong></p>
<ol class="arabic simple">
<li>Modified the contents of the <code class="docutils literal"><span class="pre">form</span></code> tag to have two <code class="docutils literal"><span class="pre">input</span></code> tags. The first is named <code class="docutils literal"><span class="pre">message</span></code> and the second is named <code class="docutils literal"><span class="pre">first_name</span></code></li>
<li>Added a check if <code class="docutils literal"><span class="pre">form_post_data</span></code> has not been defined so we don’t show the message or wording</li>
<li>Added code to display the message from the user’s <code class="docutils literal"><span class="pre">POST</span></code> action</li>
</ol>
<p>Go into the <code class="docutils literal"><span class="pre">controllers</span></code> folder now and edit the <code class="docutils literal"><span class="pre">root.py</span></code> file. There will be two functions inside of the <code class="docutils literal"><span class="pre">RootController</span></code> class which will display the <code class="docutils literal"><span class="pre">index.html</span></code> file when your web browser hits the <code class="docutils literal"><span class="pre">'/'</span></code> endpoint. If the user puts some data into the textbox and hits the submit button then they will see the personalized message displayed back at them.</p>
<p>Modify the <code class="docutils literal"><span class="pre">root.py</span></code> to look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pecan</span> <span class="kn">import</span> <span class="n">expose</span>
<span class="k">class</span> <span class="nc">RootController</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@expose</span><span class="p">(</span><span class="n">generic</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">template</span><span class="o">=</span><span class="s1">'index.html'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">()</span>
<span class="nd">@index.when</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">'POST'</span><span class="p">,</span> <span class="n">template</span><span class="o">=</span><span class="s1">'index.html'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index_post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">form_post_data</span><span class="o">=</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>What did we just do?</strong></p>
<ol class="arabic simple">
<li>Modified the <code class="docutils literal"><span class="pre">index</span></code> function to render the initial <code class="docutils literal"><span class="pre">index.html</span></code> webpage</li>
<li>Modified the <code class="docutils literal"><span class="pre">index_post</span></code> function to return the posted data via keyword arguments</li>
</ol>
<p>Run the application:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pecan serve config.py
</pre></div>
</div>
<p>Open a web browser: <a class="reference external" href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a></p>
</div>
<div class="section" id="adding-validation">
<h2>Adding Validation<a class="headerlink" href="#adding-validation" title="Permalink to this headline">¶</a></h2>
<p>Enter a message into the textbox along with a name in the second textbox and press the submit button. You should see a personalized message displayed below the form once the page posts back.</p>
<p>One problem you might have noticed is if you don’t enter a message or a first name then you simply see no value entered for that part of the message. Let’s add a little validation to make sure a message and a first name was actually entered. For this, we will use <a class="reference external" href="http://wtforms.simplecodes.com/">WTForms</a> but you can substitute anything else for your projects.</p>
<p>Add support for the <a class="reference external" href="http://wtforms.simplecodes.com/">WTForms</a> library:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pip install wtforms
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Keep in mind that Pecan is not opinionated when it comes to a particular library when working with form generation, validation, etc. Choose which libraries you prefer and integrate those with Pecan. This is one way of doing this, there are many more ways so feel free to handle this however you want in your own projects.</p>
</div>
<p>Go back to the <code class="docutils literal"><span class="pre">root.py</span></code> files and modify it like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pecan</span> <span class="kn">import</span> <span class="n">expose</span><span class="p">,</span> <span class="n">request</span>
<span class="kn">from</span> <span class="nn">wtforms</span> <span class="kn">import</span> <span class="n">Form</span><span class="p">,</span> <span class="n">TextField</span><span class="p">,</span> <span class="n">validators</span>
<span class="k">class</span> <span class="nc">PersonalizedMessageForm</span><span class="p">(</span><span class="n">Form</span><span class="p">):</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">TextField</span><span class="p">(</span><span class="sa">u</span><span class="s1">'Enter a message'</span><span class="p">,</span>
<span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">validators</span><span class="o">.</span><span class="n">required</span><span class="p">()])</span>
<span class="n">first_name</span> <span class="o">=</span> <span class="n">TextField</span><span class="p">(</span><span class="sa">u</span><span class="s1">'Enter your first name'</span><span class="p">,</span>
<span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">validators</span><span class="o">.</span><span class="n">required</span><span class="p">()])</span>
<span class="k">class</span> <span class="nc">RootController</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@expose</span><span class="p">(</span><span class="n">generic</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">template</span><span class="o">=</span><span class="s1">'index.html'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">PersonalizedMessageForm</span><span class="p">())</span>
<span class="nd">@index.when</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">'POST'</span><span class="p">,</span> <span class="n">template</span><span class="o">=</span><span class="s1">'index.html'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index_post</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">form</span> <span class="o">=</span> <span class="n">PersonalizedMessageForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
<span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">validate</span><span class="p">():</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">message</span><span class="o">=</span><span class="n">form</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">data</span><span class="p">,</span>
<span class="n">first_name</span><span class="o">=</span><span class="n">form</span><span class="o">.</span><span class="n">first_name</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>What did we just do?</strong></p>
<ol class="arabic simple">
<li>Added the <code class="docutils literal"><span class="pre">PersonalizedMessageForm</span></code> with two textfields and a required field validator for each</li>
<li>Modified the <code class="docutils literal"><span class="pre">index</span></code> function to create a new instance of the <code class="docutils literal"><span class="pre">PersonalizedMessageForm</span></code> class and return it</li>
<li>In the <code class="docutils literal"><span class="pre">index_post</span></code> function modify it to gather the posted data and validate it. If its valid, then set the returned data to be displayed on the webpage. If not valid, send the form which will contain the data plus the error message(s)</li>
</ol>
<p>Modify the <code class="docutils literal"><span class="pre">index.html</span></code> like this:</p>
<div class="highlight-html"><div class="highlight"><pre><span></span><%inherit file="layout.html" />
## provide definitions for blocks we want to redefine
<%def name="title()">
Welcome to Pecan!
</%def>
<header>
<h1><img src="/images/logo.png" /></h1>
</header>
<div id="content">
% if not form:
<p>${first_name}, your message is: ${message}</p>
% else:
<form method="POST" action="/">
<div>
${form.message.label}:
${form.message}
% if form.message.errors:
<strong>${form.message.errors[0]}</strong>
% endif
</div>
<div>
${form.first_name.label}:
${form.first_name}
% if form.first_name.errors:
<strong>${form.first_name.errors[0]}</strong>
% endif
</div>
<input type="submit" value="Submit">
</form>
% endif
</div>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Keep in mind when using the <a class="reference external" href="http://wtforms.simplecodes.com/">WTForms</a> library you can customize the error messages and more. Also, you have multiple validation rules so make sure to catch all the errors which will mean you need a loop rather than the simple example above which grabs the first error item in the list. See the <a class="reference external" href="http://wtforms.simplecodes.com/">documentation</a> for more information.</p>
</div>
<p>Run the application:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pecan serve config.py
</pre></div>
</div>
<p>Open a web browser: <a class="reference external" href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a></p>
<p>Try the form with valid data and with no data entered.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Example Application: Simple Forms Processing</a><ul>
<li><a class="reference internal" href="#project-setup">Project Setup</a></li>
<li><a class="reference internal" href="#adding-validation">Adding Validation</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="errors.html"
title="previous chapter">Custom Error Documents</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="simple_ajax.html"
title="next chapter">Example Application: Simple AJAX</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/simple_forms_processing.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="simple_ajax.html" title="Example Application: Simple AJAX"
>next</a> |</li>
<li class="right" >
<a href="errors.html" title="Custom Error Documents"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Pecan 1.2.1 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2018, Jonathan LaCour.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.6.
</div>
</body>
</html>
|