4. Initial views

Now it’s time to create our views files and add our view callables.

Every view will be decorated with a @view_config decorator.

@view_config will configure our Pyramid application by telling it how to correlate our view callables with routes, and set some restrictions on specific view resolution mechanisms. It gets picked up when config.scan() is called in our __init__.py, and all of our views are registered with our app.

Note

You could explictly configure your application with the config.add_view() method, but the approach with @view_config and config.scan() is often more convenient.

Edit our views files

Let’s make some stubs for our views. We will populate them with actual code in later chapters.

Open views/default.py and replace the current @view_config decorator and the def my_view() function with the following.

1
2
3
4
5
6
7
from pyramid.view import view_config


@view_config(route_name='home',
             renderer='pyramid_blogr:templates/index.jinja2')
def index_page(request):
    return {}

Here @view_config takes two parameters that will register our index_page callable in Pyramid’s registry, specifying the route that should be used to match this view. We also specify the renderer that will be used to transform the data which the view returns into a response suitable for the client.

The template location is specified using the asset location format, which is in the form of package_name:path_to_template, or by specifying relative path to the template.

Note

It also easy to add your own custom renderer, or use an add-on package like pyramid_mako.

The renderer is picked up automatically by specifying the file extension, like asset.jinja2/asset.jinja2 or when you provide a name, such as for the string/json renderer.

Pyramid provides a few renderers including:
  • jinja2 templates (by external package)
  • mako templates (by external package)
  • chameleon templates (by external package)
  • string output
  • json encoder

Create new file views/blog.py and add the following contents:

1
2
3
4
5
6
7
from pyramid.view import view_config


@view_config(route_name='blog',
             renderer='pyramid_blogr:templates/view_blog.jinja2')
def blog_view(request):
    return {}

This registers blog_view with a route named 'blog' using the view_blog.jinja2 template as the response.

The next views we should create are views that will handle creation and updates to our blog entries.

10
11
12
13
@view_config(route_name='blog_action', match_param='action=create',
             renderer='pyramid_blogr:templates/edit_blog.jinja2')
def blog_create(request):
    return {}

Notice that there is a new keyword introduced to the @view_config decorator. The purpose of match_param is to tell Pyramid which view callable to use when the dynamic part of the route {action} is matched. For example, the above view will be launched for the URL /blog/create.

Next we add the view for the URL /blog/edit.

16
17
18
19
@view_config(route_name='blog_action', match_param='action=edit',
             renderer='pyramid_blogr:templates/edit_blog.jinja2')
def blog_update(request):
    return {}

Note

Every view can be decorated unlimited times with different parameters passed to @view_config.

Now switch back to views/default.py, and add the following.

10
11
12
13
14
@view_config(route_name='auth', match_param='action=in', renderer='string',
             request_method='POST')
@view_config(route_name='auth', match_param='action=out', renderer='string')
def sign_in_out(request):
    return {}

These routes will handle user authentication and logout. They do not use a template because they will just perform HTTP redirects.

Note that this view is decorated more than once. It also introduces one new parameter. request_method restricts view resolution to a specific request method, or in this example, to just POST requests. This route will not be reachable with GET requests.

Note

If you navigate your browser directly to /sign/in, you will get a 404 page because this view is not matched for GET requests.

Content of views files

Here’s how our views files look at this point.

views/blog.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from pyramid.view import view_config


@view_config(route_name='blog',
             renderer='pyramid_blogr:templates/view_blog.jinja2')
def blog_view(request):
    return {}


@view_config(route_name='blog_action', match_param='action=create',
             renderer='pyramid_blogr:templates/edit_blog.jinja2')
def blog_create(request):
    return {}


@view_config(route_name='blog_action', match_param='action=edit',
             renderer='pyramid_blogr:templates/edit_blog.jinja2')
def blog_update(request):
    return {}

views/default.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from pyramid.view import view_config


@view_config(route_name='home',
             renderer='pyramid_blogr:templates/index.jinja2')
def index_page(request):
    return {}


@view_config(route_name='auth', match_param='action=in', renderer='string',
             request_method='POST')
@view_config(route_name='auth', match_param='action=out', renderer='string')
def sign_in_out(request):
    return {}

views/__init__.py is currently an empty file.

At this point we can start implementing our view code.

Next: 5. Blog models and views.