Adding extra_context in Django logout built-in vie

2019-01-27 22:50发布

问题:

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

def logout(request, next_page=None,
       template_name='registration/logged_out.html',
       redirect_field_name=REDIRECT_FIELD_NAME,
       current_app=None, extra_context=None):

I would like to add extra_context to get rid of the 'Logged out' title that appear when I log off

so I'm trying this in my url confs :

(r'^accounts/logout/$', logout(extra_context={'title':'something else'}) ),

but then I get this error : logout() takes at least 1 non-keyword argument (0 given) what I'm doing wrong? ps: when I do

(r'^accounts/logout/$', logout ),

it works, but then I get the 'Logged out' text...

Thanks, Fred

回答1:

When you write logout(extra_context={'title':'something else'}), you're actually calling logout right there in the URLconf, which won't work. Any URLconf tuple can have an optional third element, which should be a dictionary of extra keyword arguments to pass to the view function.

(r'^accounts/logout/$', logout, {'extra_context':{'title':'something else'}}),

Alternatively, you could write your own view which calls logout passing in whatever arguments you want -- that's typically how you would "extend" function-based generic views in more complicated cases.



回答2:

Adding my findings for django 2.0 as the previous answer on this thread no longer works for the most recent django version.

With 2.0, the proper way of adding a URL to your urls.py file is by using path():

from django.urls import path
from django.contrib.auth import views as auth_views

path('accounts/logout/', auth_views.LogoutView.as_view(
  extra_context={'foo':'bar'}
)),

The code snippet to highlight here is the .as_view() function. Django 2.0 implements auth views as classes. You can read more about this in the Authentication Views documentation

You then "convert" the class to a view using `.as_view() and you are able to pass in any class attributes defined in the source code as named parameters.

Passing in extra_context (which defaults to None) automatically exposes these context variables to your templates.

You can access the source code for LogoutView by following this python path: django.contrib.auth.views

Here you can see the other class attributes you can pass into LogoutView and the other auth view classes.



回答3:

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.