Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old October 19th, 2003, 03:16 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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.

Reply With Quote
  #2  
Old October 19th, 2003, 03:43 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Have fun,
Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old October 19th, 2003, 04:11 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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.

http://htmltmpl.sourceforge.net/python.html

Right now I'm trying to work out mod_rewrite more than anything else though.

Reply With Quote
  #4  
Old October 19th, 2003, 05:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Mark.

Reply With Quote
  #5  
Old October 20th, 2003, 08:55 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
wow telex!

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
Eli

Reply With Quote
  #6  
Old October 20th, 2003, 10:56 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Your favourite Python CGI tricks


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway