6. Defining views

6.1. Overview

views.py is a module in which you need to write your business logic. A single view must be callable, must have request object as a first argument, and must return an instance of werkzeug.Response.

6.1.1. The simplest view

Let’s look at the simplest view.


# -*- coding: utf-8 -*-
# The simplest view.

from werkzeug import Response
def index(request):
  return Response("Hello")

It’s super easy. What if you want to use html templates for rendering pages. Please use kay.utils.render_to_response() for this purpose:


# -*- coding: utf-8 -*-
# The simplest view.

from werkzeug import Response
from kay.utils import render_to_response

def index(request):
  return render_to_response('myapp/index.html', {'message': 'Hello'})

That’s it. Please see kay.utils.render_to_response() for more details.

6.1.2. Class based views

You can use callable object as a view. For this purpose, you can extend kay.handlers.BaseHandler and define your own handler class.

Here is a example for a simple class based view:

from kay.handlers import BaseHandler

from myapp.models import Comment

# ..

class CommentHandler(BaseHandler):
  def prepare(self):
    self.comments = Comment.all().order('-created').fetch(100)
    self.form = CommentForm()

  def get(self):
    return render_to_response('myapp/index.html',
                              { 'comments': self.comments,
                               'form': self.form.as_widget()})

  def post(self):
    if self.form.validate(self.request.form):
      if self.request.user.is_authenticated():
        user = self.request.user
        user = None
      new_comment = Comment(body=self.form['comment'],user=user)
      return redirect(url_for('myapp/index'))
    return self.get()

# If you want a fancy import name, you might want to instantiate
# here as follows:
# comment_handler = CommentHandler()

These handler must have one or more methods with a name as the same as lower-cased HTTP Methods to corresponds with. It can have prepare method to do some task before above methods whatever HTTP method the current HTTP request uses.

Table Of Contents

Previous topic

5. URL Mapping

Next topic

7. Using Jinja2

This Page