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

关于 django.auth 模块

  •  
  •   africle · 2018-06-22 08:55:19 +08:00 · 1474 次点击
    这是一个创建于 2107 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大大,小弟 django 新手,目前想做一个关于 django.auth 登陆认证的一个界面,在 model.py 中定义 UserModel 时,对于继承 User 和 AbstactUser 不知如何选择,还有,如果不用 username,仅仅使用 phoneNumber 做登陆验证,又该如何定义 Model,请不吝赐教,不胜感激!

    2 条回复    2018-06-24 21:45:22 +08:00
    xpresslink
        1
    xpresslink  
       2018-06-22 21:02:35 +08:00
    ?? 对于继承 User 和 AbstactUser 不知如何选择
    == 不可以从 User 继承,必须选择从 AbstractUser 这种抽象 Model 继承, 一是继承 AbstractUser,AbstractUser 已有了验证等方法;另一种是继承 AbstractBaseUser,AbstractBaseUser 只有基本字段,方法也要自己实现 。

    ?? 如果不用 username,仅仅使用 phoneNumber 做登陆验证

    需在 settings 加个参数指定使用那个验证的后端, 也可同时指定多个验证方式。

    AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend', # 这个是默认的 username 认证
    'account.authentication.PhoneAuthBackend',
    )
    # 在 account 这个 app 里建一个 authentication.py 创建 PhoneAuthBackend 类
    要实现两个方法,
    authenticate
    get_user

    我写的估计你看不懂,推荐你看一下《 django by example 》这本书。
    africle
        2
    africle  
    OP
       2018-06-24 21:45:22 +08:00
    @xpresslink 好的,非常感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2718 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:41 · PVG 20:41 · LAX 05:41 · JFK 08:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.