.. _simple_forms_processing:
Example Application: Simple Forms Processing
============================================
This guide will walk you through building a simple Pecan web application that will do some simple forms processing.
Project Setup
-------------
First, you'll need to install Pecan:
::
$ pip install pecan
Use Pecan's basic template support to start a new project:
::
$ pecan create mywebsite
$ cd mywebsite
Install the new project in development mode:
::
$ python setup.py develop
With the project ready, go into the ``templates`` folder and edit the ``index.html`` file. Modify it so that it resembles this:
.. code-block:: html
<%inherit file="layout.html" />
<%def name="title()">
Welcome to Pecan!
%def>
% if not form_post_data is UNDEFINED:
${form_post_data['first_name']}, your message is: ${form_post_data['message']}
% endif
**What did we just do?**
#. Modified the contents of the ``form`` tag to have two ``input`` tags. The first is named ``message`` and the second is named ``first_name``
#. Added a check if ``form_post_data`` has not been defined so we don't show the message or wording
#. Added code to display the message from the user's ``POST`` action
Go into the ``controllers`` folder now and edit the ``root.py`` file. There will be two functions inside of the ``RootController`` class which will display the ``index.html`` file when your web browser hits the ``'/'`` 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.
Modify the ``root.py`` to look like this:
.. code-block:: python
from pecan import expose
class RootController(object):
@expose(generic=True, template='index.html')
def index(self):
return dict()
@index.when(method='POST', template='index.html')
def index_post(self, **kwargs):
return dict(form_post_data=kwargs)
**What did we just do?**
#. Modified the ``index`` function to render the initial ``index.html`` webpage
#. Modified the ``index_post`` function to return the posted data via keyword arguments
Run the application:
::
$ pecan serve config.py
Open a web browser: `http://127.0.0.1:8080/
% if not form:
${first_name}, your message is: ${message}
% else:
% endif
.. note::
Keep in mind when using the `WTForms