简体   繁体   中英

base.html can't access {% if user.is_authenticated %}

In my base.html user.is_authenticated always returns false...

I want the base template to toggle between 'login and logout'

help would be much appreciated


base.html

<div id="leftsidebar">
        {% block leftsidebar %}
            {% if user.is_authenticated %}
                <p><a href="/logout/">logout</a></p>
            {% else %}
                <p><a href="/login/">Login</a></p>
            {% endif %}
        {% endblock %}
</div> <!-- end leftsidebar -->

forms.py

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from drinker.models import Drinker
from django.contrib.auth import authenticate, login, logout

class LoginForm(forms.Form):
    username        = forms.CharField(max_length=255, required=True, label=(u'User Name'))
    password        = forms.CharField(max_length=255, required=True, label=(u'Password'), widget= forms.PasswordInput(render_value=False))

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from drinker.forms import RegistrationForm, LoginForm
from django.contrib.auth.models import User
from drinker.models import Drinker
from django.contrib.auth import authenticate, login, logout

def ProfileRequest(request):
        if request.user.is_authenticated():
                return render_to_response('profile.html')
        else:
                return render_to_response('/', {'user': request.user.username})

You're not passing anything in the context when the user is authenticated, so {{ user }} is empty. You could pass it explicitly in the context dictionary, ie the second argument, but a better thing to do is to use the context processors to do that automatically: these only run if you use a RequestContext.

There is a shortcut to do this for you:

from django.shortcuts import render

...
return render(request, 'profile.html', {})

Also note the else statement there makes no sense: if the user is not authenticated, there will be no username.

Use {% if request.user.is_authenticated %} instead of {% if user.is_authenticated %}.

Your template only knows about the variables you pass to it in the context plus the request.

My logic was wrong by having the else statement,

else will never pass the user to the template because its always authenticated :P

def ProfileRequest(request):
        if request.user.is_authenticated():
                return render_to_response('profile.html')
        else:
                return render_to_response('/', {'user': request.user.username})

corrected view definition below

def ProfileRequest(request):
        if request.user.is_authenticated():
                return render(request, 'profile.html', {})

plus i had to use render properly ( thank you very much for that Daniel Roseman :) )

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