Adding extra context in built-in exit mode from Django

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

 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' header that appears when I log out

so i am trying to do this in my confl url:

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

but then I get this error: logout () takes at least 1 argument without the keyword (0) what am I doing wrong? ps: when i do

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

it works, but then I get the text "Recorded" ...

Thanks Fred

+6
source share
2 answers

When you write logout(extra_context={'title':'something else'}) , you actually call logout directly in the URLconf, which will not work. Any URLconf tuple can have an optional third element, which must be a dictionary of additional keyword arguments to go to the view function .

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

Alternatively, you can write your own view that causes logout pass any arguments you want β€” typically how you β€œextend” general views based on functions in more complex cases.

+11
source

Adding my results for django 2.0, as the previous answer in this thread no longer works for the latest version of django.

Since 2.0, the correct way to add a URL to your urls.py file is with 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 .as_view () function is highlighted in this code snippet. Django 2.0 implements auth views as classes. For more information, see Authentication Documentation.

Then you β€œconvert” the class into a view using `.as_view (), and you can pass the class attributes defined in the source code as named parameters.

Passing to extra_context (None by default) automatically provides 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 other class attributes that you can pass to LogoutView and other auth view classes.

+1
source

Source: https://habr.com/ru/post/893121/


All Articles