Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

134 lines (84 sloc) 3.31 kb

Step 06: View Classes

Free-standing functions are the regular way to do views. Many times, though, you have several views that are closely related. For example, a content type might have many different ways to look at it.

For some people, grouping these together makes logical sense. A view class lets you group views, sharing some state assignments and helper functions as class methods.

Even better, from a UX person's perspective, the methods on the view class look like a "Template API" from the inside the namespace of the view.


  • Explain the why as well as the what on view classes
  • Show how templates interact with the app via the view class


  • Move templates to their own directory
  • Understand the structure of a view class's __init__ and methods
  • See how the @reify decorator can form a "Template API"
  • Adapt template expressions to point through the view
  • Change tests to instantiate the view class then call it
  • Move the repetitive dummy data into its own module


  1. $ cd ../../creatingux; mkdir step06; cd step06
  2. (Unchanged) Copy the following into step06/
  3. Copy the following into step06/
  4. Copy the following into step06/
  5. Copy the following "global template" into step06/templates/
  6. Copy the following into step06/templates/
  7. Copy the following into step06/templates/
  8. Copy the following into step06/templates/
  9. Copy the following into step06/templates/
  10. Copy the following into step06/
  11. $ nosetests should report running 5 tests.
  12. $ python
  13. Open in your browser.

Extra Credit

  1. Why do some ZPT expressions need view. and some don't?
  2. What exactly does @reify do?
  3. Could you shorten your unit tests by making a DummyRequest () in the test's __init__?
  4. If you do an expensive calculation for one view, does that increase performance in another view that doesn't need to recalculate it?
  5. Where does @reify store the cached value?


The idea of a view class can be used to form different patterns. In this case, we want a unit of related work, join up the views for that work, and craft our own little API that our templates use.

The test writing gets a little bit harder.


  • What was the original need that spawned view classes?
  • How do other system approach the idea?
  • What is a "push page" and what need was it addressing?
Jump to Line
Something went wrong with that request. Please try again.