簡體   English   中英

Django-模板-用戶從未認證

[英]Django - template - User in never authenticated

即使用戶登錄,也永遠不會對用戶進行身份驗證。右側欄始終顯示“未登錄”。我是否需要將某些內容返回給base.html? 我該怎么做? 我是否需要在views.py中添加新功能? 但沒有base.hthl的網址。 我想念的是什么?請具體說明我在Web開發中。 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')

能夠使用

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

因為您需要使用上下文處理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM