Web development with Python 3.1

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Oct 28 15:50:25 CET 2009

Dotan Cohen a écrit :
>> *Why* on earth do you think using a templating system will give you any less
>> control on the generated HTML ?
> Because I am wrong. I have already come to that conclusion.
> I know that Python is far enough developed that if I feel that I am
> fighting it, then _I_ am in the wrong, not Python. However, I still
> need to see that Python can do what I need in a comfortable fashion.
>>> And if I need to add a variable or three in there?
>> - first solution : use string formatting
>> python 2.x example:
>> unmaintanable_html = """
>>  <html>
>>  <head>
>>    <title>%(title)s</title>
>>  </head>
>>  <body>
>>    <h1>%(h1)s</h1>
>>    <p>%(text)s</p>
>>  </body>
>>  </html>
>> """
>> data = dict(
>>  title="index",
>>  h1="Poor man's templating",
>>  text="Won't get you very far..."
>>  )
>> return HttpResponse(unmaintanable_html % data)
> That's fine for single variables, but if I need to output a table of
> unknown rows? 

What do you think templates are for ?

> I assume that return means the end of the script.

It means that this function returns an HttpResponse object.

> Therefore I should shove the whole table into a variable and then copy
> that variable to the array "data"?

If you really love feeling the pain, that's a possible solution. I for 
one would rather use a template... But once again, your choice !-)

>> - second solution: do basically the same thing with a template system -
>> which will give you much more power and options...
> I will look further into the Django templates, I promise. But I would
> still like to know how to work with Python proper.

Using the appropriate tool for the job is "proper Python". The 
appropriate tool for generating formatted text with static and dynamic 
parts is a template system. Good news, there are quite a few available 
in Python.

More information about the Python-list mailing list