2👍
Login and logout is part of Django (and AbstractUser too) so you don’t need extend this parts of code. One thing you must do is add in settings.py file this two line of code:
LOGIN_REDIRECT_URL = 'template_name'
LOGOUT_REDIRECT_URL = 'template_name'
👤ROSO
2👍
from django.contrib.auth import logout
def custom_logout(request):
print('Loggin out {}'.format(request.user))
logout(request)
print(request.user)
return HttpResponseRedirect('/restrictedpage')
This worked for me and should work for you also.
- [Django]-Show user posts in post list
- [Django]-Accessing models by variable model name in django
- [Django]-Django: m2m_changed not fired when end of relation is deleted
0👍
Here is a solution that works for Django 3+.
Replace django.contrib.admin
with my_admin
module.
INSTALLED_APPS = [
...
'my_admin.apps.AdminConfig',
# 'django.contrib.admin',
...
]
AdminConfig (my_admin/apps.py):
from django.contrib.admin.apps import AdminConfig as ContribAdminConfig
class AdminConfig(ContribAdminConfig):
default_site = 'my_admin.admin_site.AdminSite'
AdminSite (my_admin/admin_site.py):
from django.contrib.admin import AdminSite as ContribAdminSite
from django.views.decorators.cache import never_cache
class AdminSite(ContribAdminSite):
@never_cache
def logout(self, request, extra_context=None):
"""
Define your custom logout functionality here.
Checkout the super logout method to get a baseline implementation.
Log out the user for the given HttpRequest.
This should *not* assume the user is already logged in.
"""
# Your logout code here.
return super().logout(request, extra_context)
- [Django]-Django Edit Form Data: data is duplicated instead of being updated
- [Django]-How to use python-social-auth with google only?
- [Django]-Best option for Google App Engine Datastore and external database?
0👍
Django 3+: Using a custom class-based view
Template view
To avoid conflicts with django.contrib.auth
and its defaults, create a new view with a unique name. Use the built-in function django.contrib.auth.logout
to actually sign out the user.
from django.contrib.auth import logout
from django.http import HttpRequest
from django.shortcuts import render
from django.views.generic.base import TemplateView
class SignedOutView(TemplateView):
template_name = "registration/signed_out.html"
def get(self, request: HttpRequest):
logout(request)
return render(request, self.template_name)
Template
Add a new signed_out.html
template under the registration
folder.
{% extends "base.html" %}
{% block content %}
<section id="page" class="">
<p>You are signed out!</p>
<p>Click <a href="{% url 'login' %}">here</a> to sign in again.</p>
</section>
{% endblock content %}
URLs
Update your URL paths.
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
from .views import SignedOutView
urlpatterns = [
path("accounts/", include('django.contrib.auth.urls')),
path("admin/", admin.site.urls),
path("signed-out/", SignedOutView.as_view(), name="sign-out"),
]
Use the new sign-out view
<a href="{% url 'sign-out'%}">Sign out</a>
- [Django]-Django Serializer returns JSON for parent class objects only and leave child objects as same?
- [Django]-How to store JWT Token in DB with Django REST Framework
- [Django]-Django Queryset Iterator for Ordered queryset
Source:stackexchange.com