RFC: Shim to allow view classes rather than functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.conf.urls.defaults import patterns as django_patterns
from django.conf.urls.defaults import url

def patterns(*urls):
	pages = []
	
	for cls in urls:
		if isinstance(cls, type):
			inst = cls()
			pages.append(url(inst.url, inst.resource, name = inst.name))
		else:
			#oldstyle resources
			pages.append(cls)
	return django_patterns(*pages)

	
class Page(object):
	
	url = r"^$"
	
	def resource(self, request, *args, **kwargs):
		if request.method == "GET":
			return self.get(request, *args, **kwargs)
		
		if request.method == "POST":
			return self.post(request, *args, **kwargs)
	
	@property
	def	name(self):
		return "%s-%s" % (self.__class__.__name__, "page")	

More like this

  1. Django Class Views by rmt 4 years, 10 months ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  3. Ajax API class by kcarnold 5 years, 10 months ago
  4. Row-Level, URL-based permissions for FlatPages by bradmontgomery 4 years, 10 months ago
  5. Allow separation of GET and POST implementations by agore 1 year, 10 months ago

Comments

Romain Hardouin (on March 12, 2009):

Do you want to make a view in style of RoR?

#

akaihola (on March 27, 2009):

I've experimented with class-based views in one of my projects. The nasty part is when you want to start using third-party or Django's stock decorators on your views while still being able to access individual methods and attributes in your unit tests.

#

(Forgotten your password?)