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
linkbg
V2EX  ›  Python

请教一下, Django 中自己编写的认证该怎么来判断用户是不是登入了

  •  
  •   linkbg · 2016-08-13 19:09:11 +08:00 · 5385 次点击
    这是一个创建于 2818 天前的主题,其中的信息可能已经有所发展或是发生改变。
    认证模块是自己写的。然后不用要自带的装饰器 @login_required
    13 条回复    2016-08-16 09:46:01 +08:00
    jimzhong
        1
    jimzhong  
       2016-08-13 19:19:19 +08:00
    同请教。
    wellsc
        2
    wellsc  
       2016-08-13 19:27:40 +08:00 via Android
    Session
    iiduce
        3
    iiduce  
       2016-08-13 19:29:03 +08:00
    既然不用 @login_required ,就去看看它怎么写的,搬过来用哦。

    def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
    lambda u: u.is_authenticated(),
    login_url=login_url,
    redirect_field_name=redirect_field_name
    )
    if function:
    return actual_decorator(function)
    return actual_decorator
    linkbg
        4
    linkbg  
    OP
       2016-08-13 20:09:44 +08:00
    @wellsc Session 是可以,不过,我能不能写歌 Session 的装饰器?
    linkbg
        5
    linkbg  
    OP
       2016-08-13 20:10:53 +08:00
    @iiduce 也想,不过发现.is_authenticated()函数好像有点不理解,所以也就没有写
    NaVient
        6
    NaVient  
       2016-08-15 09:13:27 +08:00
    其实 Django 的 authenticated 就是给 User 对象添加了一个 BACKENDS 属性,它是被哪个模块 authenticated 验证通过的,就给它加一串类似于 django.contrib.auth.backends.ModelBackend 这样的字符串。
    linkbg
        7
    linkbg  
    OP
       2016-08-15 14:44:36 +08:00
    @NaVient 我是自己写了一个 User 。没有使用它本身 autu 中的 User ,这样也可以使用 authenticated 吗?
    NaVient
        8
    NaVient  
       2016-08-15 15:00:53 +08:00
    @linkbg 如果一定要使用 authenticated 就给你自己写的这张 User 表添加一个 Django 默认 User 表的外键。
    FreeDog
        9
    FreeDog  
       2016-08-15 16:35:28 +08:00
    @NaVient 扩展 User 的话,官方更推荐 OneToOne 关系。 https://docs.djangoproject.com/en/1.10/topics/auth/customizing/
    linkbg
        10
    linkbg  
    OP
       2016-08-15 17:00:17 +08:00 via iPhone
    @NaVient 这个算是扩展 auth 的 User 吗?
    dikT
        11
    dikT  
       2016-08-15 17:36:03 +08:00
    '''
    def login_check(func):
    def _login_check(req):
    try:
    _ = req.session['name']
    if not _:
    return HttpResponseRedirect('/blog/login')
    except:
    return HttpResponseRedirect('/blog/login')
    else:
    return func(req)
    return _login_check
    '''
    rainybowe
        12
    rainybowe  
       2016-08-15 23:00:16 +08:00
    以前的做法是自己维护登录数据,例如一张 token 表,每次登录为用户新建 token 。每次请求需要在 request header 中添加 token ,然后写一个装饰器每次校验 token 是否存在来判断是否登陆。
    NaVient
        13
    NaVient  
       2016-08-16 09:46:01 +08:00
    @linkbg 算是吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2234 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:30 · PVG 17:30 · LAX 02:30 · JFK 05:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.