django- get personal login information

Web request authentication: https: //yiyibooks.cn/xx/django_182/topics/auth/default.html

Django uses session and middleware to intercept the request  subject to the certification system.

They provide each request on a request.user attribute representing the current user. If the current user is not logged in, the property is set to AnonymousUser one instance, otherwise it will be the User instance.

View function without modification because the views.py

# Django request object will add an attribute request.user 
# If the user is not logged in -> user AnonymousUser class is an instance of the object 
# If a user is logged -> user is a User class instance object 
# request.user.is_authenticated () 
# in addition to the template variable to your template file transfer, django framework will be passed to the template file request.user

templates/base.html中

{#} Welcome message # 
    {}%% Block header_con 
        < div class = "header_con" > 
            < div class = "header" > 
                < div class = "FL is available for purchase" > Welcome to the Fresh every day! </ Div > 
                < div class = "fr" > 
                    {%}% IF user.is_authenticated 
                    < div class = "login_btn FL" > 
                        Welcome: < EM > {{user.username}} </ EM >{# User subject is automatically rendered to the template django} # 
                    </ div>
                    {% else %}
                    <div class="login_btn fl">
                        <a href="/user/login">登录</a>
                        <span>|</span>
                        <a href="/user/register">注册</a>
                    </div>
                    {% endif %}
                    <div class="user_link fl">
                        <span>|</span>
                        <A href = "user_center_info.html" > Users </ A > 
                        < span > | </ span > 
                        < A href = "cart.html" > my cart </ A > 
                        < span > | </ span > 
                        < a href = "user_center_order.html" > my orders </ a > 
                    </ div > 
                </ div > 
            </ div > 
        </div>
    {% endblock header_con %}

 

Guess you like

Origin www.cnblogs.com/yifengs/p/11611411.html