0

I am displaying same home page for both logged-in users and logged-out users. But the change is in navbar. Upon login there will be name of the user displayed. And upon logout nothing will be displayed. If I logout and click back button I see the back previous page getting displayed (i.e the one with the user name). I searched a bit about it and found even few questions about it.

But there decorators are used to prevent logged out users to to come back. SO that that page will get prevented from loading again. But here as I am displaying the same page for both the users, how to accomplish it ?? How to prevent navbar from showing user name of users upon logout and clicking back button.

My navbar code::

{% if request.user.is_authenticated %}
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ request.user.username }}</a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="{% url 'profile' request.user.username %}">Profile</a></li>
                        <li><a href="{% url 'logout' %}">Logout</a></li>
                        {% else %} {% ifequal request.path '/accounts/login/' %}
                        <li><a href="{% url 'signup' %}">Sign Up</a></li>
                    {% else %}
                        <li><a href="{% url 'login' %}">Login</a></li>
                    {% endifequal %}
                    </ul>
                </li>
            {% endif %}

UPDATE:

My home page view is

class HomeView(TemplateView):
       template_name = "home/home.html" # same template for login and logout

urls.py is

urlpatterns = [
      url(r'^$', HomeView.as_view(), name="home"),
]

I am using django.contrib.auth.urls for both login and logout operations Thanks in advance!

e4c5
  • 52,766
  • 11
  • 101
  • 134
rammanoj
  • 479
  • 8
  • 26
  • You haven't provided enough context here. but i think your logout page does not redirect to a 'post logout' or 'good bye' page. Which is the recommended pattern – e4c5 Aug 25 '18 at 05:11
  • Thanks @e4c5 Can you please elaborate it. Which context are you asking about ??. I said that after logging out on clicking back the user is able to see the logged-in navbar. And can you provide some information about 'post logout' ?? – rammanoj Aug 25 '18 at 05:14
  • I am not talking about django contexts. you haven't provided enough code fragments (like your views etc) to reach a firm conclusion. – e4c5 Aug 25 '18 at 05:16
  • @e4c5 I updated the content is it okay ? – rammanoj Aug 25 '18 at 05:23

1 Answers1

1

This is how django contrib.auth.logout is supposed to be used (this is from the manual):

from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    # Redirect to a success page.

And this is what I mentioned in my comment. When someone logs out you need to redirect to another page that can be any page of your choosing, even the previous page that the user was on

def logout_view(request):
    logout(request)
    return HttpResponseRedirect("/")

Now when the user hits the back button, they go back to the logout page, which will send them onto the home page again ad infinitum, effectively preventing them from seeing the old content.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • I think I read in the documentation somewhere the best way to do it when using `django.contrib.auth.views` - set a `LOGOUT_RIDIRECT_URL` in *settings.py* – saketk21 Aug 25 '18 at 07:55
  • 1
    That's certainly one way of doing it, but what's the best depends on the situation. Another is to pass a `next` parameter to `LogoutView` – e4c5 Aug 25 '18 at 08:49
  • Is e4c5 a reference to the Sicilian Defence? – saketk21 Aug 25 '18 at 09:09