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
targetparameter 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:
Create view state
Get a result to render
Render the result and return.
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:
menuIf we got here via a CWF Section, then we will be able to create a
cwf.views.menu.Menuobject from that section.pathThe path of the request with no leading, trailing; or duplicate slashes.
targetThe target being reached on the class.
sectionThe CWF section that is executing this view (if one was used)
base_urlrequest.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.
Getting result for a view¶
-
View.get_result(request, target, args, kwargs)[source]¶ Takes the
requestobject, thetargetthat is been called and any positional arguments and cleaned keyword arguments and returns a result that will be rendered.If the instance has an
overridemethod , then it will pass all those arguments in that function and return it’s result.Otherwise:
Use
View.has_target()to check if thetargetexists on the instance and raise an exception if it does not exist.If the
targetdoes exist, then pass it intoView.execute()along with therequest,argsand cleanedkwargsto get a result.If the result is a callable then call it with the
requestand 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_resultwill 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
targetusingView.get_target()and calling it with therequest,*argsand**kwargs.
Rendering a view¶
-
View.rendered_from_result(request, result)[source]¶ If the result being rendered is
None, then aHttp404will be raised.If the result is a list or tuple of two items, then it assumes this list represents
(template, extra)wheretemplateis the name of the template to render andextrais any extra context to render the template with.If
templateis None, thenextrais returned, otherwise it uses thecwf.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.