Skip to content

Latest commit

 

History

History
117 lines (80 loc) · 3.44 KB

typeviews.rst

File metadata and controls

117 lines (80 loc) · 3.44 KB

4: Type-Specific Views

Type-specific views by registering a view against a class.

Background

In hierarchy we had 3 "content types" (Root, Folder, and Document.) All, however, used the same view and template.

Pyramid traversal lets you bind a view to a particular content type. This ability to make your URLs "object oriented" is one of the distinguishing features of traversal, and makes crafting a URL space more natural. Once Pyramid finds the context object in the URL path, developers have a lot of flexibility in view predicates.

Objectives

  • Use a decorator @view_config which uses the context attribute to associate a particular view with context instances of a particular class.
  • Create views and templates which are unique to a particular class (a.k.a., type).
  • Learn patterns in test writing to handle multiple kinds of contexts.

Steps

  1. We are going to use the previous step as our starting point:

    $ cd ..; cp -r hierarchy typeviews; cd typeviews
    $ $VENV/bin/python setup.py develop
  2. Our views in typeviews/tutorial/views.py need type-specific registrations:

    typeviews/tutorial/views.py

  3. We have a new contents subtemplate at typeviews/tutorial/templates/contents.jinja2:

    typeviews/tutorial/templates/contents.jinja2

  4. Make a template for viewing the root at typeviews/tutorial/templates/root.jinja2:

    typeviews/tutorial/templates/root.jinja2

  5. Now make a template for viewing folders at typeviews/tutorial/templates/folder.jinja2:

    typeviews/tutorial/templates/folder.jinja2

  6. Finally make a template for viewing documents at typeviews/tutorial/templates/document.jinja2:

    typeviews/tutorial/templates/document.jinja2

  7. More tests are needed in typeviews/tutorial/tests.py:

    typeviews/tutorial/tests.py

  8. $ $VENV/bin/nosetests should report running 4 tests.
  9. Run your Pyramid application with:

    $ $VENV/bin/pserve development.ini --reload
  10. Open http://localhost:6543/ in your browser.

Analysis

For the most significant change, our @view_config now matches on a context view predicate. We can say "use this view when looking at this kind of thing." The concept of a route as an intermediary step between URLs and views has been eliminated.

Extra Credit

  1. Should you calculate the list of children on the Python side, or access it on the template side by operating on the context?
  2. What if you need different traversal policies?
  3. In Zope, interfaces were used to register a view. How do you register a Pyramid view against instances that support a particular interface? When should you?
  4. Let's say you need a more specific view to be used on a particular instance of a class, letting a more general view cover all other instances. What are some of your options?

Traversal Details <pyramid:traversal_chapter> Hybrid Traversal and URL Dispatch <pyramid:hybrid_chapter>