Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 134 lines (84 sloc) 3.31 kB
5b58ef1 @pauleveritt Moving over to official docs area
pauleveritt authored
1 =====================
2 Step 06: View Classes
3 =====================
4
5 Free-standing functions are the regular way to do views. Many times,
6 though, you have several views that are closely related. For example,
7 a content type might have many different ways to look at it.
8
9 For some people, grouping these together makes logical sense. A view
10 class lets you group views, sharing some state assignments and helper
11 functions as class methods.
12
13 Even better, from a UX person's perspective, the methods on the view
14 class look like a "Template API" from the inside the namespace of the
15 view.
16
17 Goals
18 =====
19
20 - Explain the *why* as well as the what on view classes
21
22 - Show how templates interact with the app via the view class
23
24 Objectives
25 ==========
26
27 - Move templates to their own directory
28
29 - Understand the structure of a view class's ``__init__`` and methods
30
31 - See how the ``@reify`` decorator can form a "Template API"
32
33 - Adapt template expressions to point through the view
34
35 - Change tests to instantiate the view class then call it
36
37 - Move the repetitive dummy data into its own module
38
39 Steps
40 =====
41
42 #. ``$ cd ../../creatingux; mkdir step06; cd step06``
43
44 #. (Unchanged) Copy the following into ``step06/application.py``:
45
46 .. literalinclude:: application.py
47 :linenos:
48
49 #. Copy the following into ``step06/views.py``:
50
51 .. literalinclude:: views.py
52 :linenos:
53
54 #. Copy the following into ``step06/dummy_data.py``:
55
56 .. literalinclude:: dummy_data.py
57 :linenos:
58
59 #. Copy the following "global template" into
60 ``step06/templates/global_layout.pt``:
61
62 .. literalinclude:: templates/global_layout.pt
63 :language: html
64 :linenos:
65
66 #. Copy the following into ``step06/templates/index.pt``:
67
68 .. literalinclude:: templates/index.pt
69 :language: html
70 :linenos:
71
72 #. Copy the following into ``step06/templates/about.pt``:
73
74 .. literalinclude:: templates/about.pt
75 :language: html
76 :linenos:
77
78 #. Copy the following into ``step06/templates/company.pt``:
79
80 .. literalinclude:: templates/company.pt
81 :language: html
82 :linenos:
83
84 #. Copy the following into ``step06/templates/people.pt``:
85
86 .. literalinclude:: templates/people.pt
87 :language: html
88 :linenos:
89
90 #. Copy the following into ``step06/tests.py``:
91
92 .. literalinclude:: tests.py
93 :linenos:
94
95 #. ``$ nosetests`` should report running 5 tests.
96
97 #. ``$ python application.py``
98
e07a5cc @tshepang make example links clickable, for convenience
tshepang authored
99 #. Open http://127.0.0.1:8080 in your browser.
5b58ef1 @pauleveritt Moving over to official docs area
pauleveritt authored
100
101 Extra Credit
102 ============
103
104 #. Why do some ZPT expressions need ``view.`` and some don't?
105
106 #. What exactly does ``@reify`` do?
107
108 #. Could you shorten your unit tests by making a ``DummyRequest
109 ()`` in the test's ``__init__``?
110
111 #. If you do an expensive calculation for one view,
112 does that increase performance in another view that doesn't need to
113 recalculate it?
114
115 #. Where does ``@reify`` store the cached value?
116
117 Analysis
118 ========
119
120 The idea of a view class can be used to form different patterns. In
121 this case, we want a unit of related work, join up the views for that
122 work, and craft our own little API that our templates use.
123
124 The test writing gets a little bit harder.
125
126 Discussion
127 ==========
128
129 - What was the original need that spawned view classes?
130
131 - How do other system approach the idea?
132
133 - What is a "push page" and what need was it addressing?
Something went wrong with that request. Please try again.