Extending generic view classes for common get_context_data

三世轮回 提交于 2019-12-03 09:00:24

问题


I constantly see myself having to add the same extra variable to the context of many of my views.

def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super(MyListView, self).get_context_data(**kwargs)
    # Add in the house
    context['house'] = self.get_object().house
    return context

As I don't like repeating myself, I thought I could create a new class extending the view and then I could base all my views on the new extended view class. The thing is, there are 4 classes of views I use: CreateView, UpdateView, ListView and DeleteView. Do I really have to create a new class for each of one of them?

Isn't there something like a django "base" view class? Maybe a smarter way to do this? Many thanks in advance!


回答1:


Create a Mixin:

from django.views.generic.base import ContextMixin

class HouseMixin(ContextMixin):
  def get_house(self):
    # Get the house somehow
    return house

  def get_context_data(self, **kwargs):
    ctx = super(HouseMixin, self).get_context_data(**kwargs)
    ctx['house'] = self.get_house()
    return ctx

Then in your other classes you'd use multiple inheritance:

class HouseEditView(HouseMixin, UpdateView):
  pass

class HouseListView(HouseMixin, ListView):
  pass

and so on, then all these views will have house in the context.



来源:https://stackoverflow.com/questions/10337290/extending-generic-view-classes-for-common-get-context-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!