0

即使用户已登录,用户也永远不会经过身份验证。右侧边栏始终显示 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>

视图.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')
4

1 回答 1

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

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

于 2013-11-06T14:31:39.780 回答