Django - 模板 - 从未经过身份验证的用户

时间:2013-11-06 14:27:09

标签: python django templates authentication

即使用户已登录,用户也从不进行身份验证。右侧边栏始终显示Not Loggedin。我是否需要将某些内容返回给base.html?我将如何做到这一点?我需要在views.py中使用新功能吗?但是base.hthl没有url。我缺少什么?请具体我在网络开发。 PS:我也尝试过request.user.is_loggedin和其他一些

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

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')

1 个答案:

答案 0 :(得分:3)

能够使用

{% if request.user.is_authenticated  %}

您需要在视图中执行以下操作:

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))

因为您需要使用上下文处理器。