简体   繁体   中英

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? And how will i do that ? do i need a new function in views.py ? but there is no url for base.hthl. What i am missing?Please be specific i am in web dev. PS: i also tried if request.user.is_loggedin and some other

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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