访问会话变量

时间:2015-05-10 18:31:50

标签: python django variables templates session

我已经成功构建了一个自定义用户注册系统作为一个模块,我还有一个名为article的模块。我想在文章模块的模板中显示从我的自定义用户注册模块登录的用户。 我使用{{if user.is_authenticated}}显示{{user}}来显示模板中的用户名。

我可以在自定义用户注册模板中访问它,但不能在文章模板中访问它。我想在导航栏上显示用户名相同的用户名。

我该怎么做才能在整个项目中访问用户名,而不仅仅是在呈现的模板中。

我正在使用django 1.8,我也尝试在我的views.py文件中创建会话变量,但它也适用于同一个应用程序。

项目的views.py

from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from custom_user.forms import CustomUserCreationForm
from django.utils import timezone

def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html', c)

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:
        auth.login(request, user)
        request.session['user_id'] = user.id
        request.session['user_email'] = user.email
        return render(request, "loggedin.html", {},context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect('/accounts/invalid')

def loggedin(request):
    return render_to_response('loggedin.html', {})

def invalid_login(request):
    return render_to_response('invalid_login.html')

模板base.py

<ul class="nav navbar-nav navbar-right">
                <li><a href="/subscribe/">Subscribe</a></li>
                {% if user.is_authenticated %}
                <li><a href="/">{{user}}</a></li>
                <li><a href="/accounts/logout/">Logout</a></li>
                {% else %}
                <li><a href="/accounts/login/">Login</a></li>
                {% endif %}
            </ul>

文章应用的views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext
from django.template.loader import get_template
from django.shortcuts import render_to_response
from django.conf import settings
from article.models import Article, Comment, Subscribers
from forms import ArticleForm, CommentForm, SubscriptionForm, ActivateSubscriberForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.utils import timezone
from django.core.mail import send_mail
from random import randrange
from signup.views import *
# from .forms import RegistrationForm

# Create your views here.

def articles(request):
    return render_to_response('articles.html',
        {'articles':Article.objects.all().order_by('-id'),'last':Article.objects.earliest('-pub_date')})

def article(request, article_id=1):
    return render_to_response('article.html',
        {'article':Article.objects.get(id=article_id)})

当用户通过身份验证后,将呈现loggedin.html,并且我已创建了一个链接,可转到使用相同base.py模板的文章应用。

2 个答案:

答案 0 :(得分:0)

如果您在TEMPLATE_CONTEXT_PROCESSORS中拥有'django.contrib.auth.context_processors.auth'处理器,则可以访问身份验证数据。

但是,它默认存在 - 也许您已将其删除了?

最后,文档对该主题非常有用:https://docs.djangoproject.com/en/1.8/topics/auth/default/#authentication-data-in-templates

答案 1 :(得分:0)

您实际上还没有显示任何代码,但问题几乎肯定是您渲染模板的方式。正如Marcanpilami指出的那样,user变量由模板上下文处理器自动添加;但只有在使用RequestContext呈现模板时才会运行上下文处理器。这意味着,现在确保您使用render快捷方式,而不是render_to_response

(为了完整起见,只要您传递render_to_response就可以使用context_instance=RequestContext,但使用{{1}几乎总是更容易}})。

相关问题