Commit 8663bc11 authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #16074 -- Added ContextMixin to class-based generic views to handle...

Fixed #16074 -- Added ContextMixin to class-based generic views to handle get_context_data. Thanks emyller, Luke Plant, Preston Holmes for working on the ticket and patch.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@17875 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent b4a98271
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -8,6 +8,16 @@ from django.utils.decorators import classonlymethod
logger = getLogger('django.request')


class ContextMixin(object):
    """
    A default context mixin that passes the keyword arguments received by
    get_context_data as the template context.
    """

    def get_context_data(self, **kwargs):
        return kwargs


class View(object):
    """
    Intentionally simple parent class for all views. Only implements
@@ -110,17 +120,13 @@ class TemplateResponseMixin(object):
            return [self.template_name]


class TemplateView(TemplateResponseMixin, View):
class TemplateView(TemplateResponseMixin, ContextMixin, View):
    """
    A view that renders a template.
    A view that renders a template.  This view is different from all the others
    insofar as it also passes ``kwargs`` as ``params`` to the template context.
    """
    def get_context_data(self, **kwargs):
        return {
            'params': kwargs
        }

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        context = self.get_context_data(params=kwargs)
        return self.render_to_response(context)


+0 −10
Original line number Diff line number Diff line
@@ -217,16 +217,6 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):

        return date_list

    def get_context_data(self, **kwargs):
        """
        Get the context. Must return a Context (or subclass) instance.
        """
        items = kwargs.pop('object_list')
        context = super(BaseDateListView, self).get_context_data(object_list=items)
        context.update(kwargs)
        return context


class BaseArchiveIndexView(BaseDateListView):
    """
    Base class for archives of date-based items.
+5 −4
Original line number Diff line number Diff line
@@ -2,10 +2,10 @@ from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.http import Http404
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.base import TemplateResponseMixin, ContextMixin, View


class SingleObjectMixin(object):
class SingleObjectMixin(ContextMixin):
    """
    Provides the ability to retrieve a single object for further manipulation.
    """
@@ -86,11 +86,12 @@ class SingleObjectMixin(object):
            return None

    def get_context_data(self, **kwargs):
        context = kwargs
        context = {}
        context_object_name = self.get_context_object_name(self.object)
        if context_object_name:
            context[context_object_name] = self.object
        return context
        context.update(kwargs)
        return super(SingleObjectMixin, self).get_context_data(**context)


class BaseDetailView(SingleObjectMixin, View):
+5 −7
Original line number Diff line number Diff line
from django.forms import models as model_forms
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect
from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.base import TemplateResponseMixin, ContextMixin, View
from django.views.generic.detail import (SingleObjectMixin,
                        SingleObjectTemplateResponseMixin, BaseDetailView)


class FormMixin(object):
class FormMixin(ContextMixin):
    """
    A mixin that provides a way to show and handle a form in a request.
    """
@@ -45,9 +45,6 @@ class FormMixin(object):
            })
        return kwargs

    def get_context_data(self, **kwargs):
        return kwargs

    def get_success_url(self):
        if self.success_url:
            url = self.success_url
@@ -113,13 +110,14 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
        return super(ModelFormMixin, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = kwargs
        context = {}
        if self.object:
            context['object'] = self.object
            context_object_name = self.get_context_object_name(self.object)
            if context_object_name:
                context[context_object_name] = self.object
        return context
        context.update(kwargs)
        return super(ModelFormMixin, self).get_context_data(**context)


class ProcessFormView(View):
+4 −4
Original line number Diff line number Diff line
@@ -3,10 +3,10 @@ from django.core.exceptions import ImproperlyConfigured
from django.http import Http404
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.base import TemplateResponseMixin, ContextMixin, View


class MultipleObjectMixin(object):
class MultipleObjectMixin(ContextMixin):
    allow_empty = True
    queryset = None
    model = None
@@ -103,10 +103,10 @@ class MultipleObjectMixin(object):
                'is_paginated': False,
                'object_list': queryset
            }
        context.update(kwargs)
        if context_object_name is not None:
            context[context_object_name] = queryset
        return context
        context.update(kwargs)
        return super(MultipleObjectMixin, self).get_context_data(**context)


class BaseListView(MultipleObjectMixin, View):
Loading