commit | author | age
|
b731b5
|
1 |
.. _qtut_view_classes: |
PE |
2 |
|
b1b922
|
3 |
====================================== |
PE |
4 |
09: Organizing Views With View Classes |
|
5 |
====================================== |
|
6 |
|
842a4f
|
7 |
Change our view functions to be methods on a view class, then move some |
SP |
8 |
declarations to the class level. |
|
9 |
|
b1b922
|
10 |
|
PE |
11 |
Background |
|
12 |
========== |
|
13 |
|
51f056
|
14 |
So far our views have been simple, free-standing functions. Many times your |
c62c95
|
15 |
views are related to one another. They may consist of different ways to look at or work |
51f056
|
16 |
on the same data, or be a REST API that handles multiple operations. Grouping |
SP |
17 |
these views together as a :ref:`view class <class_as_view>` makes sense: |
b1b922
|
18 |
|
842a4f
|
19 |
- Group views. |
b1b922
|
20 |
|
842a4f
|
21 |
- Centralize some repetitive defaults. |
b1b922
|
22 |
|
842a4f
|
23 |
- Share some state and helpers. |
b1b922
|
24 |
|
842a4f
|
25 |
In this step we just do the absolute minimum to convert the existing views to a |
SP |
26 |
view class. In a later tutorial step, we'll examine view classes in depth. |
|
27 |
|
b1b922
|
28 |
|
PE |
29 |
Objectives |
|
30 |
========== |
|
31 |
|
842a4f
|
32 |
- Group related views into a view class. |
b1b922
|
33 |
|
842a4f
|
34 |
- Centralize configuration with class-level ``@view_defaults``. |
SP |
35 |
|
b1b922
|
36 |
|
PE |
37 |
Steps |
|
38 |
===== |
|
39 |
|
|
40 |
#. First we copy the results of the previous step: |
|
41 |
|
|
42 |
.. code-block:: bash |
|
43 |
|
187104
|
44 |
$ cd ..; cp -r templating view_classes; cd view_classes |
fd965f
|
45 |
$ $VENV/bin/pip install -e . |
b1b922
|
46 |
|
842a4f
|
47 |
#. Our ``view_classes/tutorial/views.py`` now has a view class with our two |
SP |
48 |
views: |
b1b922
|
49 |
|
PE |
50 |
.. literalinclude:: view_classes/tutorial/views.py |
|
51 |
:linenos: |
|
52 |
|
842a4f
|
53 |
#. Our unit tests in ``view_classes/tutorial/tests.py`` don't run, so let's |
SP |
54 |
modify them to import the view class, and make an instance before getting a |
|
55 |
response: |
b1b922
|
56 |
|
PE |
57 |
.. literalinclude:: view_classes/tutorial/tests.py |
|
58 |
:linenos: |
|
59 |
|
|
60 |
#. Now run the tests: |
|
61 |
|
|
62 |
.. code-block:: bash |
|
63 |
|
|
64 |
|
842a4f
|
65 |
$ $VENV/bin/py.test tutorial/tests.py -q |
SP |
66 |
.... |
|
67 |
4 passed in 0.34 seconds |
b1b922
|
68 |
|
PE |
69 |
#. Run your Pyramid application with: |
|
70 |
|
|
71 |
.. code-block:: bash |
|
72 |
|
187104
|
73 |
$ $VENV/bin/pserve development.ini --reload |
b1b922
|
74 |
|
842a4f
|
75 |
#. Open http://localhost:6543/ and http://localhost:6543/howdy in your browser. |
SP |
76 |
|
b1b922
|
77 |
|
PE |
78 |
Analysis |
|
79 |
======== |
|
80 |
|
|
81 |
To ease the transition to view classes, we didn't introduce any new |
842a4f
|
82 |
functionality. We simply changed the view functions to methods on a view class, |
SP |
83 |
then updated the tests. |
b1b922
|
84 |
|
842a4f
|
85 |
In our ``TutorialViews`` view class, you can see that our two view classes are |
SP |
86 |
logically grouped together as methods on a common class. Since the two views |
|
87 |
shared the same template, we could move that to a ``@view_defaults`` decorator |
|
88 |
at the class level. |
b1b922
|
89 |
|
842a4f
|
90 |
The tests needed to change. Obviously we needed to import the view class. But |
SP |
91 |
you can also see the pattern in the tests of instantiating the view class with |
|
92 |
the dummy request first, then calling the view method being tested. |
b1b922
|
93 |
|
4042c7
|
94 |
.. seealso:: :ref:`class_as_view` |