Web development with Python 3.1

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


Dotan Cohen a écrit :
 >>> 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>.

I don't know how you got that result - but it's certainly *not* what I 
get here.

 > It
 > did not look like I had any control over the tags at all.

I can tell you you have full control over the whole HTTP response's 
content and headers. And FWIW, this has nothing to do with templating 
systems.

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

I use Django's templates to generate any kind of textual content - not 
only HTML. I could even use them to generate Python source code. And 
that's true for quite a few other templating systems around (Mako, 
Jinja, Cheetah, just to name a few).

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


Using which framework ? If it's about Turbogears, I don't use it so I 
can't answer. If it's using Pylons, you can use almost whatever 
templating system you want - or not use any. If it's with Django, do you 
want it with or without the templating system ?

 > I hate to ask for code, but this conversation would be easier that way.

Mmmm.... May I suggest that you first read the relevant documentation 
for the aforementionned frameworks ?

Ok, here's a Django example without a template:

# views.py
def index(request):
    torvalds = request.GET.get("torvalds", "No torvalds here")
    return HttpResponse("<html><body>%s</body></html>" % torvalds)


and here's an exemple using a template:

# templates/index.html

<html>
<body>
{{ torvalds }}
</body>
</html


# views.py
def index(request):
    torvalds = request.GET.get("torvalds", "No torvalds here")
    return render_to_response("index.html", dict(torvalds=torvalds))






More information about the Python-list mailing list