Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Guides to Pylons for people in a hurry > Pylons for people in a hurry
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Pylons for people in a hurry
Added by Brendan Arnold, last edited by Brendan Arnold on Jun 08, 2008  (view change) show comment
Labels: 

Pylons for people in a hurry

This is one of a set of quick guides for getting started with the different components of Pylons.

Introduction

Pylons is a web framework for developing web applications. Working within a web framework is useful because,

  • It ties together a bunch of useful libraries which,
    • make development quicker
    • encourage code reuse
    • establish commonality with other Pylons users
  • It defines a standard structure for your application meaning other developers (and your future self) will find it easier to understand
  • It encourages good programming practice through separation of key application components (aka. model, view and control)

Pylons itself is largely just glue code that ties the libraries together.

Model, view, control

Applications are far more understandable when we can separate out different parts in a reasonably balanced and easily understood way. One way to tackle this is using the model, view, control (MVC) paradigm.

  • The model is your data, the data storage structure and code which directly provides the database API - i.e. SQLAlchemy
  • The view is what gets delivered to the browser such as the xml, (x)html, javascript, css etc. and the code that generates this - i.e. Mako
  • The control is all the code in between that processes the data as it passes between the database (model) and the browser output (view)

Installing Pylons

See main docs on Installing Pylons on how to do this. UNIX users are advised to sandbox install Pylons to avoid clashes between the easy_install package management system and the OS package management system.

Basic use of Pylons

We start by creating a skeleton Pylons application named 'MyApp' using Paste. At the command-line type

paster create -t pylons MyApp

This creates a standard file structure. Listed below are the most import files,

MyApp/
  development.ini
  docs/
  myapp/
    config
    controllers
    lib
    model
    public
    templates
    tests

We will be mostly concerned with the contents of the myapp directory.

Next, cd to the MyApp directory and start off the Paste server by entering the following on the command line,

paster serve --reload development.ini

We can now visit http://127.0.0.1:5000/ to see a static page served up by the Paste server (actually myapp/public/index.html).

Next we create a controller using the following command,

paster controller my_controller

This creates a file named my_controller.py in myapp/controllers. Notice there is a method named index().

Visit http://127.0.0.1:5000/my_controller/index in your browser to see 'Hello World' rendered from index() in the MyController class.

  • index() is an action of the controller MyController

Now try http://localhost:5000/my_controller/my_action - it doesn't work.

Enter the following as another method of the MyController class after the index() method and try the last link again.

1
2
def my_action(self):
    return "Lorum ipsum yadda yadda"

We have just defined a new action in the controller. Typically each action handles a different task in the application. For example, one displays a list of database contents, one displays a form to edit content, another saves items to the database.

At the top of top of my_controller.py add the following import,

1
from datetime import datetime

and revise my_action() so it reads,

1
2
3
def my_action(self):
    time_string = datetime.now().strftime("%H:%M:%S")
    return time_string

Hit refresh a few time on your browser to see the time being updated. The page is now truly dynamic!

Our controller now needs a way to receive input submitted from the browser.

Accessing submitted data in the controller

Data from a URI or a HTML form is contained in a dictionary called request.params[] with keys that correspond to the name attribute of the form item. i.e.

1
2
3
4
<form action="http://localhost:5000/my_controller/index" method="POST">
  <input type="text" name="age" />
  <button type="submit">Submit</button>
</form>

once submitted, request.params['age'] will contain whatever was entered into the input field.

Preserving data between actions

Once the HTML has been generated and sent to the browser, the program ends and variables are reset. However, say we wish to preserve the age entered by the form from the previous example,

1
2
3
4
def my_action(self):
  session['age'] = request.params['age']
  session.save()
  return 'Hello World'

session is a dictionary-like object that remains intact between requests. The next action called would find session['age'] set to 32.

  • session.save() is necessary to preserve the state

Further reading

Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top