简体   繁体   English

基于django的用户身份验证

[英]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: 我了解如何验证django中的用户是否已登录以及他们的会话是否在以下情况下处于活动状态:

if user is not None and user.is_active:

My issue is my django templates specifically with the section with Register | Login 我的问题是我的django模板,特别是“ Register | Login ”部分Register | Login Register | Login that look like: Register | Login如下所示:

                <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 is_authenticated() django.contrib.auth.user模型中的辅助方法

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. 另外,请注意is_active标志用于检查用户是否处于活动状态,并且应用于检查用户是否可以成功登录系统。

You could also access the current logged in user using request.user.is_authenticated in the template. 您还可以使用模板中的request.user.is_authenticated访问当前登录的用户。

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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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