Edgewall Software

Changes between Version 9 and Version 10 of GenshiTutorial


Ignore:
Timestamp:
Aug 29, 2007, 1:01:38 PM (17 years ago)
Author:
cmlenz
Comment:

Started submission form part

Legend:

Unmodified
Added
Removed
Modified
  • GenshiTutorial

    v9 v10  
    22
    33This tutorial is intended to give an introduction on how to use Genshi in your web application, and present common patterns and best practices. It is aimed at developers new to Genshi as well as those who've already used Genshi, but are looking for advice or inspiration on how to improve that usage.
    4 
    5 [[PageOutline(2-3, Content, inline)]]
    64
    75== Introduction ==
     
    1412 * For persistence, we'll use native Python object serialization (via the `pickle` module), instead of an SQL database and an ORM.
    1513 * There's no authentication of any kind. Anyone can submit links, anyone can comment.
     14
     15[[PageOutline(2-3, Content, inline)]]
    1616
    1717== Prerequisites ==
     
    247247}}}
    248248
     249== Extending the Template ==
     250
    249251Now let's change the `Root.index()` method to pass the submissions list to the template:
    250252
     
    291293
    292294In the previous step, we've already added a link to a submission form to the template, but we haven't implemented the logic to handle requests to that link yet.
     295
     296To do that, we need to add a method to the `Root` class in `geddit/controller.py`:
     297
     298{{{
     299#!python
     300    @cherrypy.expose
     301    def submit(self, cancel=False, **data):
     302        if cherrypy.request.method == 'POST':
     303            if cancel:
     304                raise cherrypy.HTTPRedirect('/')
     305            # TODO: validate the input data!
     306            submission = Submission(**data)
     307            self.data.append(submission)
     308            raise cherrypy.HTTPRedirect('/')
     309
     310        tmpl = loader.load('submit.html')
     311        stream = tmpl.generate()
     312        return stream.render('html', doctype='html')
     313}}}
     314
     315And of course we'll need to ad a template to display the submission form. In `geddit/templates`, create a file named `submit.html`, with the following content:
     316
     317{{{
     318#!genshi
     319<html xmlns="http://www.w3.org/1999/xhtml"
     320      xmlns:py="http://genshi.edgewall.org/">
     321  <head>
     322    <title>Geddit: Submit new link</title>
     323  </head>
     324  <body>
     325    <h1>Geddit</h1>
     326
     327    <h2>Submit new link</h2>
     328    <form action="" method="post">
     329      <table summary=""><tr>
     330        <th><label for="username">Your name:</label></th>
     331        <td><input type="text" id="username" name="username" /></td>
     332      </tr><tr>
     333        <th><label for="url">Link URL:</label></th>
     334        <td><input type="text" id="url" name="url" /></td>
     335      </tr>
     336      <tr>
     337        <th><label for="title">Title:</label></th>
     338        <td><input type="text" name="title" /></td>
     339      </tr></table>
     340      <div>
     341        <input type="submit" value="Submit" />
     342        <input type="submit" name="cancel" value="Cancel" />
     343      </div>
     344    </form>
     345
     346  </body>
     347</html>
     348}}}
     349
     350Now, if you click on the “Submit new link” link on the start page, you should see the submission form. You can enter values in the form fields and submit the form, which will take you back to the start page, where you'll see that your link has been added to the list. Or you can click on the “Cancel” button, which will take you back to the start page, but not add a link.
     351
     352Please note though that we're not performing ''any'' kind of validation on the input, and that's of course a bad thing. So let's add validation next.