This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
django-oopviews / README.rst
| 6002677b » | zerok | 2008-09-30 | 1 | =============== | |
| 2 | django-oopviews | ||||
| 3 | =============== | ||||
| 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 | 12 | alternative:: | |
| 6002677b » | zerok | 2008-09-30 | 13 | ||
| 14 | from django_oopviews.base import create_view, BaseView | ||||
| 15 | |||||
| 16 | class View1(BaseView): | ||||
| 17 | def __init__(self, request, \*args, \*\*kwargs): | ||||
| 18 | # Here you have your common code | ||||
| 19 | self.my_variable = 1 | ||||
| 20 | def __call__(self, request, \*args, \*\*kwargs): | ||||
| 21 | whatever = self.my_variable + 1 | ||||
| 22 | return HttpResponse(whatever) | ||||
| 23 | |||||
| 24 | class View2(View1): | ||||
| 25 | def __call__(self, request, \*args, \*\*kwargs): | ||||
| 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/\*'. | ||||







