public
Description: django-oopviews provides a simple way to have some object orientation in Django's view layer
Homepage:
Clone URL: git://github.com/zerok/django-oopviews.git
commit  8b80cae437b6089310ae12dd76532624c84db18b
tree    455706c02fb0a5dfcb29572779c2dde34ecb3c1c
parent  2ed44332279daefbae0804f9f060901c38ff34cd
django-oopviews / README.rst
2ed44332 » zerok 2008-10-01 Added some versioning histo... 1 ###############
6002677b » zerok 2008-09-30 Initial commit 2 django-oopviews
2ed44332 » zerok 2008-10-01 Added some versioning histo... 3 ###############
6002677b » zerok 2008-09-30 Initial commit 4
5 .. contents::
6
7 Base-implementation
8 ===================
9
10 In some instances you end up producing tons of views that actually do mostly
11 the same except for perhaps one or two lines. This module offers you a simple
e3202e73 » zerok 2008-09-30 Fixed some small typos in t... 12 alternative::
6002677b » zerok 2008-09-30 Initial commit 13
14 from django_oopviews.base import create_view, BaseView
15
16 class View1(BaseView):
60ecccc4 » zerok 2008-09-30 Fixed another bunch of typo... 17 def __init__(self, request, *args, **kwargs):
6002677b » zerok 2008-09-30 Initial commit 18 # Here you have your common code
19 self.my_variable = 1
60ecccc4 » zerok 2008-09-30 Fixed another bunch of typo... 20 def __call__(self, request, *args, **kwargs):
6002677b » zerok 2008-09-30 Initial commit 21 whatever = self.my_variable + 1
22 return HttpResponse(whatever)
23
24 class View2(View1):
60ecccc4 » zerok 2008-09-30 Fixed another bunch of typo... 25 def __call__(self, request, *args, **kwargs):
6002677b » zerok 2008-09-30 Initial commit 26 return HttpResponse(self.my_variable)
27
28 view1 = create_view(View1)
29 view2 = create_view(View2)
30
31 In this example, the code in ``View1.__init__`` is shared between View1 and
32 View2, so you don't need to write it again.
33
34 If you want to share some HttpResponse post-processing, implement the
35 ``BaseView.__after__(self, response_obj)`` method
36
37 For more details check out this `blog post`_
38
39 .. _blog post: http://zerokspot.com/weblog/1037/
40
41 Addons
42 ========
43
44 Content-Type-Negotitation with OOPViews
45 ---------------------------------------
46
47 In some situations it comes in handy, to do some content type negotiation
48 to really provide an optimized view for the user depending on what a user's
49 application supports (say WML or HTML or XML over HTML). HTTP/1.1 handles
50 this using the "Accept"-request header to give the user the option, to say
51 what kind of content type she'd prefer or give a list of content types
52 prioritized with a value between 0 and 1.
53
54 This abstract view class should demonstrate, how you can easily handle such
55 situations within Django purely in the view code. The idea is pretty simple:
56 Simply use the ``__call__`` method as dispatcher for content-type-specific
57 methods.
58
59 To use this code, simply inherit the basic implementation and then specify
60 your content-type-specific methods and register them in the
61 ``ctn_accept_binding``-dictionary::
62
63 from django.http import HttpResponse
64 from django_oopview import ctn
65
66 class TestView(ctn.AbstractCTNView):
67 ctn_accept_binding = {
68 'text/html': 'html',
69 'text/*': 'html',
70 '*/*': 'html',
71 }
72
73 def html(self, request, *args, **kwargs):
74 return HttpResponse("Hello", mimetype='text/html')
75
76 The ``ctn_accept_binding``-dictionary not only allows you to bind a method to a
77 content-type, but if you set a value to a tuple instead of just a string, it
78 will take the first element of that tuple as a priority value similar to the
79 one used in the "Accept"-handling. This way, you can prioritize methods for
80 the case, that the user requests any type of a given family like for instance
81 'text/\*'.
2ed44332 » zerok 2008-10-01 Added some versioning histo... 82
83 History
84 ========
85
86 0.2 (Oct 1 2008)
87 comes as its own library using setuptools and offering with the
88 django_oopviews.ctn module a simple implementation of content negotiation
89 in HTTP using OOPViews.
90
91 0.1 (inofficial)
92 This version only included the BaseView as well as the create_view
93 function and was bundled with the django-zsutils library.
94