1

I am using the django's built-in auth module for user authentication.

urls.py

url(r'^accounts/logout/', auth_views.logout, kwargs={'next_page': reverse_lazy('login')}),
url(r'^accounts/login/', auth_views.login, name='login', kwargs={'redirect_authenticated_user': True,"authentication_form":CustomAuthForm}),

The user is redirected to login page immediately after logout. But I need to display a "signed out!" message to the user in the login page.

How would I know whether a user clicked the logout link to arrive at login page or he/she directly visited login(in this case I won't show any message).

Streetway Fun
  • 223
  • 2
  • 10
  • Why don't you use javascript to check if the button has clicked or not and then display your message? Here's an answer of including javascript in django admin: [stackoverflow](https://stackoverflow.com/questions/19910450/how-to-load-a-custom-js-file-in-django-admin-home) – Shameer Kashif Jun 24 '18 at 07:33
  • @Shiri How would javascript help if a button is clicked from a page other than the current one leading to a redirect to the current page? Please go through the question again. – Streetway Fun Jun 24 '18 at 08:02

2 Answers2

0

Sounds like the Django messages framework is something you could use here - in the logout.post, you'd need to add a message messages.success(request, 'Successfully logged out.') that would be rendered by your login template.

umgelurgel
  • 312
  • 1
  • 6
0

You can use The messages framework from Django, like this:

from django.contrib.messages import get_messages

def logout(request, ...):

    # Logout the user
    # Add a logout message
    messages.add_message(request, messages.SUCCESS, "signed out!")

    return HttpResponseRedirect(...)

def login(request, ...):
    storage = get_messages(request)
    for message in storage:
        msg = message
        break
    return render(request, 'your_app/login.html', {'message': msg})

Then in your login.html template, check if message is not None and display it. Inject this where you want the message to be displayed

{% if message %}
<div>
   {{ message }}
</div>
{% endif %}
Youssef BH
  • 534
  • 5
  • 14