Instead of using a function for your views, this allows you to use a class. For your urls definition it works just as it normally does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class View(object):
def __new__(cls, request, **kwargs):
obj = super(View, cls).__new__(cls)
return obj(request, **kwargs)
def __init__(self):
pass
def __call__(self, request, **kwargs):
pass
class GetPostView(View):
def __call__(self, request, **kwargs):
if request.POST:
return self.Post(request, **kwargs)
return self.Get(request, **kwargs)
def Get(self, request, **kwargs):
pass
def Post(self, request, **kwargs):
pass
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Nice. Just what the doctor ordered.
#
Please login first before commenting.