Skip to content

HTTPS clone URL

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.

Goals

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

Objectives

  • 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

Steps

  1. $ cd ../../creatingux; mkdir step06; cd step06
  2. (Unchanged) Copy the following into step06/application.py:
  3. Copy the following into step06/views.py:
  4. Copy the following into step06/dummy_data.py:
  5. Copy the following "global template" into step06/templates/global_layout.pt:
  6. Copy the following into step06/templates/index.pt:
  7. Copy the following into step06/templates/about.pt:
  8. Copy the following into step06/templates/company.pt:
  9. Copy the following into step06/templates/people.pt:
  10. Copy the following into step06/tests.py:
  11. $ nosetests should report running 5 tests.
  12. $ python application.py
  13. Open http://127.0.0.1:8080 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?

Analysis

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.

Discussion

  • 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.