Chapter 1: Introducing Pylons +++++++++++++++++++++++++++++ .. index :: single: Chap 1-Introducing Pylons This book is about Pylons, an exciting, modern web development framework that puts the developer firmly in control and makes building complex web applications as easy as possible. Pylons has grown hugely in popularity in recent years because of its careful balance of powerful development features and its modular internal architecture, which help developers to quickly create sophisticated web applications without hiding what is really going on behind the scenes. Pylons gives you the power you need to efficiently create web sites and web applications while also being flexible enough to allow you to do things differently when you really need to. Best of all, Pylons is an open source project with a great community behind it to offer help and support when you need it. The first part of this book will give you all the knowledge you need to start using Pylons’ default configuration to build high-quality production web sites. In Part 2, you’ll learn about some of Pylons’ more advanced features, such as Unicode and internationalization support, Ajax, and URL routing, before moving on to Part 3 to learn about expert topics such as the Web Server Gateway Interface, authentication and authorization, deployment, and logging. Each chapter will serve as a complete guide to each of the topics covered and will contain links to areas you can go for further information. Throughout the book, I will also be taking you through how to develop a simple web site application called SimpleSite so that you can see how the principles described in each of the chapters apply in a real Pylons application. The Old Way: CGI Scripts ======================== In the past, developers typically wrote web applications as a series of simple scripts, each of which would be responsible for accessing the database for the data it needed and generating HTML to produce the pages it output. Each individual script was quick to write and easy for an experienced developer viewing the code for the first time to understand, because everything relevant to the generation of a particular page would be in one script. Developers had direct access via SQL to the database they were using and had the power and flexibility to write their code in whichever way was appropriate for their needs. .. index :: single: CGI script example; listings Here’s a simple example of the way CGI scripts used to be written: :: #!/usr/bin/env python # Get the configuration for the script import ConfigParser config = ConfigParser.ConfigParser() config.read('/path/to/config.ini') # If debugging is enabled, set up the cgitb module if config.get('general', 'debug') == 'on': import cgitb; cgitb.enable() # Begin the non-configuration-dependent imports import cgi import MySQLdb import os # Output the HTTP headers print "Content-type: text/html\n\n" # Output the head of the HTML page print "
Here are the comments:" # Get the ID from the URL based on the QUERY_STRING environment # variable using the cgi module fields = cgi.FieldStorage() page = int(fields['page'].value) # Fetch data from the database connection = MySQLdb.connect( db=config.get('database', 'database'), user=config.get('database', 'user'), passwd=config.get('database', 'password'), host=config.get('database', 'host') ) cursor = connection.cursor() cursor.execute("SELECT id, data FROM comment WHERE page=%s", (page,)) results = cursor.fetchall() cursor.close() connection.close() # Output the comments for id, data in results: print "