如何在许多不同的 Django 视图中注入相同的上下文?

How to inject same context in many different Django views?

我想在多个视图中放置关于一个对象的信息,而不是在每个视图的 get_context_data 中重复它。如您所知,我需要一个 class 和 get_context_data 内部,我可以将其与其他视图混合。 在我的示例中,我想在 UpdateAnotherObjectView:

的上下文中看到 'some_object'
class BaseObjectInfoView(View):
    def get_context_data(self, **kwargs):
        context_data = super(BaseObjectInfoView, self).get_context_data(**kwargs)
        context_data['some_object'] = SomeObjects.objects.get(pk=1)
        return context_data

class UpdateAnotherObjectView(BaseObjectInfo, UpdateView):
    template_name = 'create_object.html'
    form_class = AnotherObjectForm
    model = AnotherObjects

    def get_context_data(self, **kwargs):
        context_data = super(UpdateAnotherObjectView, self).get_context_data(**kwargs)
        context_data['all_another_objects'] = AnotherObjects.objects.all()
        return context_data

它有效,但 get_context_data 不是父 'View' class 的一部分。可能我需要更特殊的 class 来继承 BaseObjectInfoView?

或者用另一种方法构建上下文可能更好?

也许这样更容易阅读...

def add_context(func):
# this is a wrapper function
    def wrapper(*args, **kwargs):
        context_data = func(*args, **kwargs)
        context_data['some_object'] = SomeObjects.objects.get(pk=1)
        return context_data
return wrapper


class UpdateAnotherObjectView(BaseObjectInfo, UpdateView):
    template_name = 'create_object.html'
    form_class = AnotherObjectForm
    model = AnotherObjects

    @add_context
    def get_context_data(self, **kwargs):
        kwargs['all_another_objects'] = AnotherObjects.objects.all()
        return kwargs

Mixins 不需要是视图,但如果它们有覆盖的方法,IDE 会有所帮助。

上下文由 django.views.generic.base.ContextMixin 处理(详情见 this very handy site),因此基于 class 的视图方式是这样的:

from django.views import generic

class WebsiteCommonMixin(generic.base.ContextMixin):
    page_title = ''
    active_menu = None

    def get_context_data(self, **kwargs):
        context = super(WebsiteCommonMixin, self).get_context_data(**kwargs)
        context.update(dict(page_title=self.page_title, active_menu=self.active_menu))
        return context

class NewsListView(WebsiteCommonMixin, ListView):
    page_title = 'News list'
    active_menu = 'News'
    model = News
    paginate_by = 12

我为许多项目都这样做,无论如何您必须创建的简单视图都是完全声明性的。简单来说,我的意思是它们可以由多个 mixin 组成,所有这些都在 get_queryset、get_context_data 或 form_valid 中完成困难的事情。更详细的示例,直接来自项目:

class FeedbackMixin(object):
    message = 'Well Done!'

    def __init__(self):
        self._message_kwargs = {}
        super().__init__()

    def add_message_kwarg(self, name, value) -> None:
        self._message_kwargs[name] = value

    def format_message(self, kwargs) -> str:
        return self.message.format(**kwargs)

    def generate_message(self) -> None:
        msg = self.format_message(self._message_kwargs)
        messages.success(getattr(self, 'request'), msg)


class ModelFeedbackMixin(FeedbackMixin, generic.edit.ModelFormMixin):
    success_view_name = None
    success_url_kwargs = None

    def get_success_url_kwargs(self):
        return self.success_url_kwargs

    def get_success_url(self) -> str:
        success_url_kwargs = self.get_success_url_kwargs()
        if not self.success_view_name:
            url = super().get_success_url()
        elif success_url_kwargs is not None:
            url = reverse(self.success_view_name, kwargs=success_url_kwargs)
        else:
            if hasattr(self.object, 'slug'):
                url_kwargs = {'slug': self.object.slug}
            else:
                url_kwargs = {'pk': self.object.pk}
            url = reverse(self.success_view_name, kwargs=url_kwargs)
        return url

    def form_valid(self, form):
        response = super().form_valid(form)
        self.generate_message()
        return response