Web development with Python 3.1
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Wed Oct 28 10:17:47 EDT 2009
Dotan Cohen a écrit :
>>>>> While I know that to be true in the general sense, from what I've
>>>>> looked at Django and other frameworks it seems that the web frameworks
>>>>> push the coder to use templates, not letting him near the HTML.
>>>> Well... there must be a reason, for sure... No ?
>>>>
>>> ֹYes, but I don't like it.
>> Why so ? What's your problem with templating systems, exactly ?
>>
>
> They don't give me the control over the HTML that I would like. I'm
> probably stuck in 1998 when they didn't exist, but I like to write the
> HTML myself.
<once-again>
*Why* on earth do you think using a templating system will give you any
less control on the generated HTML ?
</once-again>
>
>>>>> Django and the other frameworks seem to
>>>>> force the user to use templates.
>>>> Not at all. Nothing prevents you from shooting yourself in the foot and
>>>> generating HTML "by hand" in your view functions (or request handler
>>>> methods
>>>> etc, depending on the framework).
>>> How is this done in Django, then?
>> def index(request):
>> unmaintanable_html = """
>> <html>
>> <head>
>> <title>Index</title>
>> </head>
>> <body>
>> <h1>Embedded HTML is a PITA</h1>
>> <p>but some like pains...</p>
>> </body>
>> </html>
>> """
>> return HttpResponse(unmaintanable_html)
>>
>
> 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)
- second solution: do basically the same thing with a template system -
which will give you much more power and options...
> Static HTML I can
> do without Python.
Sorry, I forgot to setup a database etc for this example !-)
More information about the Python-list
mailing list