我在基本模板中打印会话变量,如下所示:
{% if request.session.user_id %}
Welcome {{ request.session.user_id }}, <a href="/accounts/logout/">Logout</a>
{% endif %}
views.py
def loginfun(request):
username = request.POST['username']
password = request.POST['password']
errors = []
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
request.session['user_id'] = username
return render_to_response('home.html', context_instance=RequestContext(request))
else:
#errors.append("Account Inactive")
return render_to_response('home.html',{'errors':errors}, context_instance=RequestContext(request))
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration'
)
答案 0 :(得分:0)
django.core.context_processors.request
默认不在TEMPLATE_CONTEXT_PROCESSORS
中,因此您根本没有{{ request }}
变量