I am looking for a Class Base Views introduction or tutorial.
I have googled the web, and all I was able to find was the Generic Class
Base Views.
There were only a few examples of non generic views, however the code was to
fuzzy.
Django generally has a superb documentation, but this part is not so good.
Q1. Or maybe I am wrong and there is no Class Based Views beside the Generic
ones?
I want to move my function based views to the classes.
I would like to make them RESTful with login_required decorator for some of
them:
I have this:
import re
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic.base import TemplateView
nonalpha_re = re.compile('[^A-Z]')
class BaseView(TemplateView):
""" Subclass this and add GET / POST / etc methods. """
allowed_methods = ('GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS')
context_object_name = "object"
def __call__(self, request, *args, **kwargs):
""" """
method = nonalpha_re.sub('', request.method.upper())
if not method in self.allowed_methods or not hasattr(self, method):
return self.http_method_not_allowed(request, *args, **kwargs)
return getattr(self, method)(request, *args, **kwargs)
class AuthenticatedView(BaseView):
""" Require login """
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(BaseView, self).dispatch(*args, **kwargs)
and then in my views.py
class ContractView(AuthenticatedView):
template_name = "crm/contract/index.html"
def GET(request, id):
c = get_object_or_404(Contract, id=id)
return {"contract": c}
and so on...
Q2. Am I thinking right?
Q3. How to make prettier JSON dumps?
for example if I set ContractView.mimetype = 'application/json'
I would like to render returned value from GET method and send it to browser
as a JSON string instead of directing it to the template.
Looking forward to hearing from you.
Sincerely
Matt Harasymczuk
http://www.matt.harasymczuk.pl
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.