Adding extra_context in Django logout built-in view

前端 未结 3 1787
情书的邮戳
情书的邮戳 2020-12-19 05:56

In django/contrib/auth/views.py there is the definition of the logout view :

def logout(request, next_page=None,
       template_name=\'registration/logged_o         


        
3条回答
  •  醉梦人生
    2020-12-19 07:01

    I had a similar problem with titles and generic views in django 1.11 (though the problem was mostly that I didn't switch docs version from 2.0). I wanted to pass title via extra_context to the view inherited from CreateView, and discovered that django's generic view had no such attribute. So, here are my crutches:

    1. Create custom mixin (hope that's more or less what ContextMixin in 2.0 does):

      class ExtraContextMixin():
          extra_context = {}
      
          def get_context_data(self, **kwargs):
              context = super().get_context_data(**kwargs)
              context.update(self.extra_context)
      
              return context
      
    2. Add mixin to view's ancestors (that's all code I had to change):

      class CustomView(ExtraContextMixin, CreateView):
      
    3. Pass extra_context from url:

      url(r'^custom-view/$', views.CustomView.as_view(extra_context={'title': 'just any'}), name='custom-view')
      

    Unfortunately, I have no idea whether such solution is acceptable (no need since 2.0, obviously), but at least it's working.

提交回复
热议问题