Base View

class cwf.views.base.View[source]

Base class for cwf views

Django only requires a callable object to represent a view, which means you either provide a function, or an instance of a class that implements the special __call__ method.

Django provides it’s own class based views where one class represents one view.

CWF class based views were created before this idea came around and does it a little differently in that it represents a collection of views and which view is used when the instance of the class is called is determined by the target parameter which is a string representing the name of the method to use.

This means you can use CWF view as follows:

# in webthing.somesection.views

from cwf.views import View
class MyAwesomeView(View):
    def thing(self, request):
        return "path/to/template.html", {}

To define the view and either the following with a Section

# in webthing.somesection.urls

from cwf.sections import Section

section = Section('').configure(''
     , kls = "MyAwesomeView"
     , target = "thing"
     , module = 'webthings.somesection.views'
     )

urlpatterns = section.patterns()

Or if you prefer a more manual approach to your urlpatterns:

# in webthing.somesection.urls

from webthing.somesection.views import MyAwesomeView
from django.conf.urls.defaults import *

urlpatterns = patterns(''
    , ( '^$', MyAwesomeView(), {'target':'thing'})
    )

Note

You can create as many or as little instances of a View class as you wish as long as you don’t put any state on the instance.

Calling a view

View.__call__(request, target, *args, **kwargs)[source]

When an instance of the view class is called, it will do the following:

View State

Rather than keeping state on the View instance, CWF will use the get_state method to create an object that you can use to store state for the request.

The view will create this state object when the view is being called and make it available from request.state.

View.get_state(request, target)[source]

Return an object that can be used to store state for a request.

For convenience, this object behaves like a Javascript object (supports both dot notation and array notation for accessing and setting variables).

When it’s created, it is initialized with some values:

menu

If we got here via a CWF Section, then we will be able to create a cwf.views.menu.Menu object from that section.

path

The path of the request with no leading, trailing; or duplicate slashes.

target

The target being reached on the class.

section

The CWF section that is executing this view (if one was used)

base_url

request.META.get('SCRIPT_NAME', '')

Cleaning View kwargs

All keyword arguments to the view will be cleaned as according to the clean_view_kwarg method before being passed into the target.

View.clean_view_kwargs(kwargs)[source]

Replace all kwargs with the result of passing them through clean_view_kwarg

View.clean_view_kwarg(key, item)[source]

Clean a single view kwarg

If it’s a string, make sure it has no trailing slashes , otherwise just return it as is.

Getting result for a view

View.get_result(request, target, args, kwargs)[source]

Takes the request object, the target that is been called and any positional arguments and cleaned keyword arguments and returns a result that will be rendered.

If the instance has an override method , then it will pass all those arguments in that function and return it’s result.

Otherwise:

Use View.has_target() to check if the target exists on the instance and raise an exception if it does not exist.

If the target does exist, then pass it into View.execute() along with the request, args and cleaned kwargs to get a result.

If the result is a callable then call it with the request and return what that gives back.

Otherwise, return the result as is.

View.override(request, target, args, kwargs)

Not defined by default on the BaseView

If implemented, then get_result will use this as a short circuit to skip the normal machinery

View.execute(target, request, args, kwargs)[source]

Execute target with the request, args and kwargs.

By default this means getting a callable for the target using View.get_target() and calling it with the request, *args and **kwargs.

View.has_target(target)[source]

Return whether view has specified target

By default, just use hasattr on the instance

View.get_target(target)[source]

Return callable for the target from this instance.

By default, just use getattr on the instance.

Rendering a view

View.rendered_from_result(request, result)[source]

If the result being rendered is None, then a Http404 will be raised.

If the result is a list or tuple of two items, then it assumes this list represents (template, extra) where template is the name of the template to render and extra is any extra context to render the template with.

If template is None, then extra is returned, otherwise it uses the cwf.views.rendering.Renderer.render() to render the template and context.

If the result to render is not a two item tuple or list, then it just returns it as is.