Web development with Python 3.1

Dotan Cohen dotancohen at gmail.com
Wed Oct 28 20:42:17 CET 2009


> I've already given you that for TG:
>
> class RootController(BaseController):
>
>   @expose()
>   def page(self, torwalds=None):
>       return "<html><body><p>%s</p></body></html>" % (torwalds if torwalds
> else ""
>

Does return mean that this could not be used in the middle of a page?


> Of course nobody would do it that way. Instead, you'd define a template
>
> <html
> xmlns:py='http://genshi.edgewall.org/'><body><p>${torvalds}</p></body></html>
>
>
> And then your code becomes:
>
>
>
>   @expose("your.template.path")
>   def page(self, torwalds=None):
>       return dict(torwalds=torwalds)
>
>
> Separation of code from HTML-code. Which most people agree is a good thing.
> And no worries about having to make torwalds a string.
>

Yes, I like to separate HTML from code. However, often I need to
output something in the middle of the page. How could I do that with a
template?


> Now let's say you want torwalds to be not-empty, and a number:
>
>   @expose("your.template.path")
>   @validate(dict(torwalds=Int(not_empty=True)),
> error_handler=some_error_handler_controller_action)
>   def page(self, torwalds=None):
>       return dict(torwalds=torwalds)
>

That is nice, I will read more about the error_handler functions. Thanks.


-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il



More information about the Python-list mailing list