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. Declaring django views like web.py views by danigm 3 years, 3 months ago
  2. Cache Backend using memcached including prefix settings by mojemeno123 10 months, 4 weeks ago
  3. View decorator to convert DoesNotExist (ObjectDoesNotExist) exceptions into Http404 exceptions by jammycakes 3 years, 7 months ago
  4. Url filter middleware by limodou 6 years, 2 months ago
  5. Ajax API class by kcarnold 4 years, 11 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?)