Once an user logged out of the site, it should redirect to the home page and to display the message as \"U are successfully logged out\" in the top of the page. Anyone help
Try using sessions. Can be simpler.
In the logout view, set an entry in the session variable, like session['just_logged_out'] = True
, and in the home page view, check for the variable.
try:
just_logged_out = request.session.get('just_logged_out',False)
except:
just_logged_out = False
In the template, you can use
{% if just_logged_out %} You are successfully logged out {% endif %}
give a try to:
from django.contrib.auth.views import LogoutView
class YourCustomLogoutView(LogoutView):
def get_next_page(self):
next_page = super(YourCustomLogoutView, self).get_next_page()
messages.add_message(
self.request, messages.SUCCESS,
'You successfully log out!'
)
return next_page
in urls:
url(r'^logout/$', YourCustomLogoutView.as_view(), {'next_page': settings.LOGOUT_REDIRECT_URL}, name='logout'),
Here is a simplified version of the answer from @andilabs:
from django.contrib import messages
from django.contrib.auth.views import LogoutView
class UserLogoutView(LogoutView):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
messages.info(request, "You have successfully logged out.")
return super().dispatch(request, *args, **kwargs)
And in your urls.py:
path('logout/', views.UserLogoutView.as_view(), name='logout')
You could use the user_logged_out signal combined with the messages framework:
First, make sure the messages framework is set up (https://docs.djangoproject.com/en/dev/ref/contrib/messages/).
Then include this code somewhere that will be called (I tend to put it in a receivers.py module and then import it from a models.py file in an installed app):
from django.contrib.auth.signals import user_logged_out
from django.dispatch import receiver
from django.contrib import messages
@receiver(user_logged_out)
def on_user_logged_out(sender, request, **kwargs):
messages.add_message(request, messages.INFO, 'Logged out.')
Use the messages framework. https://docs.djangoproject.com/en/dev/ref/contrib/messages/