在我base.html文件user.is_authenticated總是返回false ...base.html文件無法訪問{%如果user.is_authenticated%}
我想要的基本模板之間切換「登錄和註銷」
的幫助會非常讚賞
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})