0

When someone clicks on my site's logout link, I want them to be redirected to the login page but I want a message to pop up saying that they were successfully logged out. However, I don't want this behaviour to occur when someone normally visits the login page.

I decided to pass in a query string so now when someone hits logout they are redirected to users/login/?logout. How can I check for this in my template though? I want to do something like:

{% if ______ %}
*Message box appears*
{% endif %}

Thanks!

3
  • Try if logout in request.GET, it it does not work try to add a value to the logout parameter (?logout=1) and then if request.GET.logout in the template. The request object should be available if the view uses a RequestContext (default if you are calling render or inheriting from a class based view) Commented May 24, 2017 at 20:27
  • The latter worked, thanks! Commented May 24, 2017 at 20:37
  • Django has this functionality built in. It's called "messenges" Commented May 24, 2017 at 21:05

2 Answers 2

1

Well, you can simply use the built-in Django messaging feature!

Here's a simple example:

# views.py

from django.contrib import messages

def register(request):
  # Logic
  # ...

  messages.success(request, 'Registered successfully!')

  # ...
  # More logic

-

# example.html
{% if messages %}
  <ul class="messages">
    {% for message in messages %}
      <li class="{{ message.tags }}">
        {{ message|safe }}
          <a href="" class="close">X</a>
      </li>
    {% endfor %}
  </ul>
{% endif %}

There's a more in-depth documentation available on Django's official docs website: https://docs.djangoproject.com/en/1.11/ref/contrib/messages/

Sign up to request clarification or add additional context in comments.

Comments

0

Referring to this document, I believe what you are looking for is this

{% if not request.user.is_authenticated %}
    <p>You are logged out!</p>
{% endif %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.