V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pythonfan
V2EX  ›  Python

django 的用户登录验证模块,为什么登录之后在模板页面取不到 user?

  •  
  •   pythonfan · 2014-06-25 14:50:21 +08:00 · 4261 次点击
    这是一个创建于 3607 天前的主题,其中的信息可能已经有所发展或是发生改变。
    自定义了AUTHENTICATION_BACKENDS,重写了authenticate方法,改完之后可以正常登录了,但是在模板页却取不到user的值,求指教。。。
    登录方法部分代码如下:
    if request.method == 'POST':
    form = LoginForm(request.POST)
    if form.is_valid():
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    # cd = form.cleaned_data
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
    user_login(request, user)

    print user.username
    return HttpResponseRedirect('/index')

    在首页的模板页面,<label class="search_label">欢迎您:{{user.username}}</label>
    一直取不到user的值
    9 条回复    2014-06-26 14:46:02 +08:00
    messense
        1
    messense  
       2014-06-25 14:58:05 +08:00
    Django Auth 的 user 是在 request.user 上的,试试 render(request, 'index.html', { 'user': request.user })
    kenis
        2
    kenis  
       2014-06-25 15:00:57 +08:00
    {{ request.user.username }}
    guoqiao
        3
    guoqiao  
       2014-06-25 15:22:13 +08:00
    @messense
    @kenis
    一直都是在模板里直接访问user的,写request.user不累么?
    只需要确保如下的配置存在:

    TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    )

    而这个是Django默认配置.
    楼主访问不到user, 看看是不是修改了这个默认配置.
    pythonfan
        4
    pythonfan  
    OP
       2014-06-25 15:54:20 +08:00
    @guoqiao 已经加了呢,直接用user.username取不到值。。。
    pythonfan
        5
    pythonfan  
    OP
       2014-06-25 15:54:37 +08:00
    @kenis 这样确实可以,谢谢
    pythonfan
        6
    pythonfan  
    OP
       2014-06-25 15:56:35 +08:00
    @messense 转到某个具体页面是直接可以访问的,但是重定向到某个view方法,直接访问user.username就不行。。。
    guoqiao
        7
    guoqiao  
       2014-06-25 16:08:38 +08:00
    几个猜测:
    1. 你的user_login方法是自带的那个login方法的别名还是自己写的?
    2. 你在authenticate中创建user时, is_active有设置吗? 如果没有,这里的user_login是调用不到的.
    3. authentication backend除了要写authenticate方法外,还要写另外一个方法:

    def get_user(self, user_id):
    try:
    return User.objects.get(pk=user_id)
    except User.DoesNotExist:
    return None
    pythonfan
        8
    pythonfan  
    OP
       2014-06-26 14:38:45 +08:00
    @guoqiao
    1、user_login是自带的方法
    2、is_active为True
    3、get_user方法也已经加了

    跟这个HttpResponseRedirect('/index')是不是有关系呢?我直接转到一个页面上是可以取到的。
    guoqiao
        9
    guoqiao  
       2014-06-26 14:46:02 +08:00
    @pythonfan
    有点意思.
    如果代码在github上,给个地址. 仅靠这段没有缩进的代码, 信息还是太少了.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1264 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:08 · PVG 02:08 · LAX 11:08 · JFK 14:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.