On Thu, Dec 15, 2011 at 8:17 PM, Bernardo <jbv...@gmail.com> wrote:

> I still need to see how to combine multiple Class based views to treat
> my data.
>

The idea here is to use Mixins, i.e., classes that implement a few methods
and can be mixed with one of your main classes.


# ------------------------------------------------------------------------
 # example.py
# ------------------------------------------------------------------------
from django.core.serializers.json import simplejson, DjangoJSONEncoder

class JSONMixin(object):
    def get(self, context, **kwargs):
        """
        On ajax requests, return a JSON object with
        information provided by subclasses.

        """

        if self.request.is_ajax():
            self.get_data(context, **kwargs)
            return HttpResponse(self.dump_data(self.data))

        return super(JSONMixin, self).get(context)

    def dump_data(self, data):
        return simplejson.dumps(data, cls=DjangoJSONEncoder)

    def get_data(self, *args, **kwargs):
        """
        Subclasses must implement this, setting the 'data' attribute on the
        instance which will then be dumped on the HttpResponse.

        """
        raise NotImplementedError

    def fail(self, msg=""):
        return HttpResponseBadRequest(msg)


# ------------------------------------------------------------------------

Assuming the code above (not sure how good it is), slap this onto another
generic CBV and, as long as that view implements a get_data which sets a
value for the 'data' instance attribute, you should be able to fetch some
json through an ajax request, e.g.


# ------------------------------------------------------------------------
# views.py
# ------------------------------------------------------------------------
from example import JSONMixin
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.views.generic import ListView

class UsersWithCount(JSONMixin, ListView): # note how we mix the
functionality
    """
     My Simple View
    """"
    foo = 'bar'

    def get(self, *args, **kwargs):
        # the usual get() logic goes here,
        # this is a very silly example
        if self.foo in self.request.GET:
                context = self.get_context_data(*args, **kwargs)
                return self.render_to_response(context)

        else:
                return HttpResponseRedirect("/")

    def get_data(self, *args, **kwargs):
        # a different logic that creates a json-dumpable object
        # and saves it in self.data
        users = User.objects.all()
        self.data = {'users': users, 'user_count': len(users)}


# ------------------------------------------------------------------------


Let us know if you have any more questions.


Cheers,
AT

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to