October 19th, 2003, 04:16 PM
Your favourite Python CGI tricks
I'm starting a project in my spare time to "port" a web development framework I wrote in Perl to Python, before I start two new contracts sometime soon. Whilst porting my existing ideas, I'm also interested in ideas others have developed over the years to make deploying web sites easier.
At the moment my favourite trick is to keep a separate module, subs.py, containins all the frequently re-used functions and code. Each script is then a matter of calling those functions, grabbing data from a form or a database (and I have a series of mysql wrapper functions because typing query handling code is dull dull dull), building an object with the resultant content, then spitting it into a function that prints the html including a template. I've found it makes writing scripts really quick, because I can focus on the logic and put the boring stuff to the back of my mind.
I'm also looking into Apache's mod_rewrite functionality to allow me to use URLs like www.site.com/users/tom/settings rather than long, ugly query strings like users.py?id=tom&op=settings.
What are your tricks? I'll post my new subs.py when it's done.
October 19th, 2003, 04:43 PM
It sounds like an interesting in this idea telex, maybe you could explain further? if your looking for any help your welcome to email me
Anyway like you, whenever i have to write a CGI app in Python I find it helpful to make a module to contain all the functions i need in the app.. though i usually name for the app/page i.e page.html.header() would call the header function for the script and etc.
Also where possable i created a template page with some special tags in like:
<template>Error message or some default text</template>
Then read the template file in and use a simple regex to output content into the webpage.. not really much of a "trick" but hey
October 19th, 2003, 05:11 PM
netyan, I used to use my own homebrew templating system in Perl until I discovered HTML::Template. I've come across htmltmpl for Python which does a similar job - you should have a look, it's a great way to simplify separating code from content.
Right now I'm trying to work out mod_rewrite more than anything else though.
October 19th, 2003, 06:03 PM
Looks cool, maybe i'll give it a go when i next do some Python CGI , it seems like a bit of an overkill for simple stuff and since the templating i'm doing right now is just read -> replace -> print
I usually don't use 3rd party modules if i can do the it myself.. i prefer to use my own code rather than somone else's, if only for the fun factor! But thanks for the recomendation, i'll keep it in mind
October 20th, 2003, 09:55 AM
M$ spent so many years developing .net and I found it quite interesting that they used that same idea exactly - seperating html from the "code behind" the html . I'm very surprised (and very very impressed) to see it in python
Cheers for the link
October 20th, 2003, 11:56 AM
Well of course the main difference between languages like Perl & Python and languages like PHP & ASP is that in the latter two, the code is embedded in the HTML, whereas in Perl and Python the HTML is embedded in the code. I find both solutions annoying though, because I like to develop web site systems that I can then use to deploy many different web sites, just changing certain forms and the HTML template. It also allows companies or groups to work together easily, assinging graphics, HTML work and coding to different groups without worrying about putting it all together.