-2

I want to prevent a user from seeing certain links on my navigation bar if the user is not logged in.

I am using the if statement in my template to do this. When I am logged in it shows the correct set of links but when I signed out it does not.

enter image description here

It should show the ul with the sign in links. WHat am I doing wrong?

This is my code :

<html>
    <head>
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">


            {% block head %}
            {% endblock %}

    </head>

    <body>
    <style>

    ul, li {

        margin: 0px 14px;
    }

    </style>
        <nav class = "navbar fixed-top navbar-light bg-light justify-content-between flex-nowrap flex-row">


        <div class  = " container">


        <a class = "navbar-brand float-left" href = "{% url 'Identities:nest'%}">nest</a>


  {% if user.is_authenticated %}

            <ul class = "nav navbar-nav flex-row float-left ">

                        <li class = "nav-item "><a class = "nav-link" href = "{% url 'Identities:logout'%}">Sign Out</a></li>
                        <li class = "nav-item"><a class = "nav-link" href = "{% url 'Identities:view_profile' %}">view Identity </a></li>
                        <li class = "nav-item"><a class = "nav-link" href = "{% url 'Identities:edit_profile' %}">edit Identity </a></li>
            </ul>

{% else %}

            <ul class = "nav navbar-nav flex-row float-left ">

            <li class = "nav-item "><a class="nav-link" href = "{% url 'Identities:login'%}">Sign In</a></li>

            </ul>


    {% endif %}
        </div>

        </nav>

    {% block body %}

            {% endblock %}

    </body>

</html>
Waltham WECAN
  • 481
  • 4
  • 11
  • 26
  • 1
    Read https://stackoverflow.com/questions/39905210/django-user-permission-inside-template#39906318 and follow the links for further explaination – dahrens Oct 01 '17 at 15:32
  • Possible duplicate of [Django user permission inside template](https://stackoverflow.com/questions/39905210/django-user-permission-inside-template) – BrettJ Oct 02 '17 at 16:24

2 Answers2

1

this example will show log in for Anonymous users and logout for logged in users:

<nav>
<!--your code-->...
<div class="nav navbar-right">
{% if user.is_authenticated %} <!--check if the user is logged in-->
<a href="log-in-url">Log out</a>
{% else %} <!--if not logged in (Anonymous user)-->
    <a href="log-in-url">Log in</a>
{% endif %}
</div>
</nav>
mohammedgqudah
  • 568
  • 4
  • 15
  • qudah I not sure this will allow access to sign link from the nav-bar. Could you explain further? I want to show certain nav-links only when the user is logged in and certain links when the user is logged out. I am think the navbar is the problem. I may be wrong – Waltham WECAN Oct 01 '17 at 23:30
  • please read the answer carefully because it do what you want : ) – mohammedgqudah Oct 08 '17 at 15:29
  • Hello I realized that the error was in my urls.py file. You are correct the answer does what I want. – Waltham WECAN Oct 08 '17 at 23:39
0

Change your if statement to the following:

{% if request.user.is_authenticated %}

That should fix you.

csling
  • 326
  • 1
  • 8