简体   繁体   English

Django-模板-用户从未认证

[英]Django - template - User in never authenticated

User is never authenticated even when the user is logged in. Right sidebar always displays Not Loggedin.Do i need to return something to base.html? 即使用户登录,也永远不会对用户进行身份验证。右侧栏始终显示“未登录”。我是否需要将某些内容返回给base.html? And how will i do that ? 我该怎么做? do i need a new function in views.py ? 我是否需要在views.py中添加新功能? but there is no url for base.hthl. 但没有base.hthl的网址。 What i am missing?Please be specific i am in web dev. 我想念的是什么?请具体说明我在Web开发中。 PS: i also tried if request.user.is_loggedin and some other PS:我还尝试了request.user.is_loggedin和其他

base.html base.html

<div id="sidebar">
    {% block sidebar %}
    <ul>
        <li><a href="/notes/all">Notes</a></li>

    </ul>
    {% endblock %}
</div>

<div id="rightsidebar">
    {% block rightsidebar %}

        {% if request.user.is_authenticated  %}
            Loggedin
        {% else %}
            Not Loggedin
        {% endif %}


    {% endblock %}
</div>

<div id="content">
    {% block content %}This is the content area{% endblock %}


</div>

views.py views.py

def auth_view(request):
    username = request.POST.get('username','')
    password = request.POST.get('password','')
    user = auth.authenticate(username = username, password = password)

    if user is not None:
        if user.is_active:
            auth.login(request,user)
            return HttpResponseRedirect('/accounts/loggedin')
        else:
        return HttpResponseRedirect('/accounts/auth_view')
else:
    return HttpResponseRedirect('/accounts/invalid')

To be able to use 能够使用

{% if request.user.is_authenticated  %}

You need to do the following in you view: 您需要在视图中执行以下操作:

from django.template import RequestContext

def view(request):
    my_data_dictionary = {}
    # code here
    return render_to_response('template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))


def view(request):
    # code here
    return render_to_response('template.html', {},
                          context_instance=RequestContext(request))

Because you need to use context processors. 因为您需要使用上下文处理器。

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

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