简体   繁体   中英

User Authentication based on if logged in or not django

I have an issue with my application I'm building. I understand how to validate if a user in django is logged in or not and whether their session is active with:

if user is not None and user.is_active:

My issue is my django templates specifically with the section with Register | Login Register | Login that look like:

                <div id="subnav_registrationLogin">
                    <ul>
                    {% block block_containersupernav %}
                        <li><span><a href="/Register" title="Register for Account">Register</a></span></li>
                        <li style="border:none;"><span><a href="/Login" title="Login To Account">Login</a></span></li>
                    {% endblock block_containersupernav %}
                    </ul>
                </div><!-- /subnav_registrationLogin -->

The issue is, my template is static and for this little code snippet above needs be more dynamic like:

if user is not None and user.is_active:
   Log Out

elif:
   <div id="subnav_registrationLogin">
   <ul>
   {% block block_containersupernav %}
      <li><span><a href="/Register" title="Register for Account">Register</a></span></li>
      <li style="border:none;"><span><a href="/Login" title="Login To Account">Login</a></span></li>
   {% endblock block_containersupernav %}
   </ul>
</div><!-- /subnav_registrationLogin -->

How can I achieve this within a template? and if I cannot within a template, how should I be doing this? Thank you!

Templates are rendered based on the context. So try this:

{% if user.is_authenticated %}
    <a href="{% url logout %}">Logout</a>
{% else %}
    <div id="subnav_registrationLogin">
       <ul>
       {% block block_containersupernav %}
          <li><span><a href="/Register" title="Register for Account">Register</a></span></li>
          <li style="border:none;"><span><a href="/Login" title="Login To Account">Login</a></span></li>
       {% endblock block_containersupernav %}
       </ul>
    </div>
{% endif %}

is_authenticated() is a helper method in the django.contrib.auth.user model

Also, note that is_active flag is used to check if the user is active or not, and should be used to check if the user can be successfully logged into the system or not.

You could also access the current logged in user using request.user.is_authenticated in the template.

So something like this?

{% if user.is_authenticated %}
<li><a href="{% url "logout" %}">Logout</a></li>
{% else %}
<div id="subnav_registrationLogin">
   <ul>
   {% block block_containersupernav %}
      <li><span><a href="/Register" title="Register for Account">Register</a></span></li>
      <li style="border:none;"><span><a href="/Login" title="Login To Account">Login</a></span></li>
   {% endblock block_containersupernav %}
   </ul>
</div><!-- /subnav_registrationLogin -->
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM