Login

Simple Class Based View Wrapper

Author:
mattjmorrison
Posted:
June 2, 2010
Language:
Python
Version:
1.2
Tags:
class based views
Score:
0 (after 2 ratings)

After reading this article: http://blog.roseman.org.uk/2010/06/2/class-based-views-and-thread-safety/

and checking out this snippet: http://djangosnippets.org/snippets/2046/

I realized that I was using a class based view without even thinking about the consequences... so, without having to completely re-factor my existing class based views to make them singletons, I wrote this wrapper that should allow my class based views to work as I intended, and maintain state for a single request only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
"Django Extension View utilities"

class ClassView():
    """
    this acts as a 'buffer' to make class based views thread safe
    usage:
    urlpatterns = patterns('accounts',
        url(r'^new/$', ClassView(views.NewBuild), name="new"),
    )
    """

    def __init__(self, class_name):
        "store the class name in an instance variable"
        self.class_name = class_name

    def __call__(self, request, *args, **kwargs):
        """each time the class_view is invoked - for each request
        new-up a class_name and call it"""
        view = self.class_name()
        return view(request, *args, **kwargs)

More like this

  1. Alternative to Class Based Views by sleepycal 2 years, 7 months ago
  2. Convenient class based views by eallik 4 years, 11 months ago
  3. YUI Autocomplete by pigletto 7 years, 8 months ago
  4. Class-Based AJAX fallback view by fahhem 4 years, 3 months ago
  5. Complex Form Preview by smagala 6 years ago

Comments

Please login first before commenting.