Personal tools
You are here: Home Documentation Tutorials Grok Poller Tutorial The First View

The First View

We will now implement the first view of the poller application; which will list all the polls for the users, and provide a link to add new polls.
This tutorial shows how to implement a simple polling application using Grok.
Page 5 of 14.

The first view is implemented in the app module, which is the template module created by grokproject. The following code is the template view created for us:

class Index(grok.View):
    pass # see app_templates/

This is the simplest view possible, which works by using some implicit conventions built in Grok views. Grok views automatically finds out some configuration parameters for the views by the contents of the modules the views are in. In this case, the Index view knows that its context is the Poller model which is the only model implemented in that module, the name of this view is index which is the lower case name of the class, and it's template is read from app_templates/ file -- more specifically <module_name>_templates/<viewclass_name>.pt.

All views in Grok has a context, which ties that view to a specific class or interface (interfaces are explained later in this tutorial). The context is the object for which this view is "attached" to. By default, the URLs in Grok are generated by traversing the object database, as if it was a file system; so when we instantiated the Poller application in the admin UI as poller, the URL for it became http://localhost:8080/poller. All views whose context is Poller, can now be accessed through that URL; for example to access the Index view in this case, open http://localhost:8080/poller/index (index is also the default view name so the previous URL also works). The traversal system will be visitited in a bit more detail later.

You can also declare the view configuration explicitly, overriding the implicit conventions. The following is the same view, with the same configuration, but declared explicitly:

class Index(grok.View):

grok.context() etc. are called Grok directives, (a full list of them can be found at Grok reference documentation [1]); in short, the directives is the way to configure Grok objects as an alternative to Zope Configuration Markup Language (ZCML) [2], which is not covered in this tutorial at all.

[1]Grok reference on directives <>
[2]"The Zope Configuration Markup Language is an XML-based language for configuring Zope software and components" -- <>

The view context is usually a grok.Model or a grok.Container class. We implemented two models in the previous section, and the grok template implements a simple container with the name of our application (the Poller class in The containers in Grok are also persistent objects like the models -- their simple purpose is to act as a container for models (the containers actually are models too.) grok.Container implements a Python dict like interface for adding models to them, and to show this in action, we will use Grok debug console to add a simple Poll object to our instance of the Poller.

To launch the debug console, run bin/poller-debug in a terminal. This starts the Python interactive interpreter, but with Grok and ZODB initialized and running; thus providing a simple access to the database and to simulate requests to the server. The default database in Grok has a root object, which is a container itself, and it's where the admin UI instantiated our application. To access the Poller instance, type:

>>> poller = root['poller']
>>> poller
< object at 0x234c5f0>

as you can see, the containers uses the dictionary interface, and it returns Python objects directly from the database. Next we will create a Poll instance manually and add it to the poller container:

>>> from poller.poll import Poll, PollOption
>>> poll = Poll()
>>> poll.question = u'How do you do?'

create a couple of options and set them to the poll:

>>> option1 = PollOption()
>>> option1.label = u"I'm okay"
>>> option2 = PollOption()
>>> option2.label = u"I feel bad"
>>> poll.options = [option1, option2]

finally add the new poll to the container:

>>> poller['how_do_you_do'] = poll

commit the changes to the ZODB (not necessary in Grok application code):

>>> import transaction
>>> transaction.commit()