Simplified the quickstart, and updated to use formencode for form validation.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from pecan import expose, request
|
||||
from pecan import expose
|
||||
from formencode import Schema, validators as v
|
||||
from webob.exc import status_map
|
||||
|
||||
@@ -9,13 +9,23 @@ class SampleForm(Schema):
|
||||
|
||||
|
||||
class RootController(object):
|
||||
@expose('index.html')
|
||||
def index(self, name='', age=''):
|
||||
return dict(errors=request.pecan['validation_errors'], name=name, age=age)
|
||||
|
||||
@expose(
|
||||
generic = True,
|
||||
template = 'index.html'
|
||||
)
|
||||
def index(self):
|
||||
return dict()
|
||||
|
||||
@expose('success.html', schema=SampleForm(), error_handler='/index')
|
||||
def handle_form(self, name, age):
|
||||
return dict(name=name, age=age)
|
||||
@index.when(
|
||||
method = 'POST',
|
||||
template = 'success.html',
|
||||
schema = SampleForm(),
|
||||
error_handler = '/index',
|
||||
htmlfill = dict(auto_insert_errors = True, prefix_error = False)
|
||||
)
|
||||
def index_post(self, name, age):
|
||||
return dict(name=name)
|
||||
|
||||
@expose('error.html')
|
||||
def error(self, status):
|
||||
|
||||
@@ -7,41 +7,36 @@
|
||||
|
||||
## now define the body of the template
|
||||
<header>
|
||||
<h1>Welcome to Pecan</h1>
|
||||
<h1><img src="/images/logo.png" /></h1>
|
||||
</header>
|
||||
<p>This is the default Pecan project, which includes:</p>
|
||||
|
||||
<ul>
|
||||
<li>The ability to serve static files.</li>
|
||||
<li>Templating using the <a href="http://www.makotemplates.org">Mako template engine</a>.</li>
|
||||
<li>Built-in error management using <a href="http://pypi.python.org/pypi/WebError">WebError</a>.</li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
Note that most functionality is enabled/disabled in your <code>app.py</code>,
|
||||
which can be edited to suit your needs, and to add additional WSGI middleware.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To get an idea of how to develop applications with Pecan,
|
||||
here is a simple form:
|
||||
</p>
|
||||
|
||||
<form method="POST" action="/handle_form" class="${'invalid' if errors else ''}">
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="name">What is your name</label></td>
|
||||
<td><input name="name" value="${name}" class="${'invalid' if error_for('name') else ''}" /></td>
|
||||
<td>${error_for('name')}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="age">How old are you?</label></td>
|
||||
<td><input name="age" value="${age}" class="${'invalid' if error_for('age') else ''}" /></td>
|
||||
<td>${error_for('age')}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<div id="content">
|
||||
|
||||
<p>This is a sample Pecan project.</p>
|
||||
|
||||
<p>
|
||||
Instructions for getting started can be found online at <a
|
||||
href="http://pecanpy.org" target="window">pecanpy.org</a>
|
||||
</p>
|
||||
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
<p>
|
||||
To get an idea of how to develop applications with Pecan,
|
||||
here is a simple form:
|
||||
</p>
|
||||
|
||||
<form method="POST" action="/">
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="name">Your Name:</label></td>
|
||||
<td><input name="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="age">Your Age:</label></td>
|
||||
<td><input name="age" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
## now define the body
|
||||
<header>
|
||||
<h1>Success!</h1>
|
||||
<h1><img src="/images/logo.png" /></h1>
|
||||
</header>
|
||||
<p>Your form submission was successful! Thanks, ${name}!</p>
|
||||
<p><a href="/">Go Back</a></p>
|
||||
|
||||
@@ -2,7 +2,6 @@ body {
|
||||
background: #311F00;
|
||||
color: white;
|
||||
font-family: 'Helvetica Neue', 'Helvetica', 'Verdana', sans-serif;
|
||||
font-size: 1.25em;
|
||||
padding: 1em 2em;
|
||||
}
|
||||
|
||||
@@ -15,17 +14,18 @@ a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
div#content {
|
||||
width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
form {
|
||||
margin: 0 1em;
|
||||
padding: 1em;
|
||||
border: 5px transparent;
|
||||
}
|
||||
|
||||
form.invalid {
|
||||
border: 5px solid FAFF78;
|
||||
}
|
||||
|
||||
input.invalid {
|
||||
input.error {
|
||||
background: #FAFF78;
|
||||
}
|
||||
|
||||
@@ -36,4 +36,4 @@ header {
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
font-family: 'Futura-CondensedExtraBold', 'Futura', 'Helvetica', sans-serif;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
pecan/templates/project/public/images/logo.png
Normal file
BIN
pecan/templates/project/public/images/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
Reference in New Issue
Block a user