| 295 | |
| 296 | To 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 | |
| 315 | And 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 | |
| 350 | Now, 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 | |
| 352 | Please 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. |