Web development with Python 3.1

Diez B. Roggisch deets at nospam.web.de
Wed Oct 28 09:46:39 EDT 2009


Dotan Cohen wrote:

>>> I insist on handling the HTML myself.
>>
>> I just don't get how embedding HTML in applicative code - a well-known
>> antipattern FWIW - relates to "handling the HTML yourself". Can you
>> *please* explain how a templating system prevents you from "handling the
>> HTML" yourself.
>>
> 
>>From the little that I played with Django, it looked like I would (for
> the sake of argument I am just using the term output, but the
> intention should be clear) output "Hello, world!" and Django would
> mangle that into <html><body><p>Hello, world!</p></body></html>. It
> did not look like I had any control over the tags at all.
> 
> 
>>>> And
>>>> even if not, what you will do is ... code your own webframework.
>>>
>>> That is why I am looking for a class that handles the backend stuff,
>>> but lets _me_ handle the HTML.
>>
>> For God's sake : *why on earth do you believe that using a templating
>> system will make you loose _any_ control on the HTML code ???*
>>
> 
> Because that's what I've seen of them.
> 
> 
>>>> And at least pylons/TG2 lets you return whatever you want instead, as a
>>>> string. Not via "print" though - which is simply only for CGI, and no
>>>> other
>>>> means (e.g. mod_wsgi) of python-web-programming.
>>>>
>>>
>>> I am trying to follow you here. What is "return whatever you want"?
>>> Return HTML to stdout to be sent to the browser?
>>
>> please leave stdout out of here - it's HTTP, so what we have are HTTP
>> requests and HTTP responses - not stdin nor stdout. Using these streams
>> as a mean of communication between the web server and the applicative
>> code is just plain old low-level GCI stuff. Your application code
>> shouldn't have any knowledge of this implementation detail - it should
>> receive (a suitable representation of) an HTTP request, and generate (a
>> suitable representation of) an HTTP response.
> 
> Can you provide a code example of printing the variable "torvalds" in
> the GET request?
> http://example.com/page.py?torvalds=tux
> Should return this:
> <html><body><p>tux</p></body></html>
> 
> And something similar for a POST request?
> 
> I hate to ask for code, but this conversation would be easier that way.
> Thanks.

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 ""


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.

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)



Diez



More information about the Python-list mailing list