推荐学习书目
› 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 · Jun 22, 2018 · 2687 views
    This topic created in 3018 days ago, the information mentioned may be changed or developed.

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

    2 replies  •  2018-06-24 21:45:22 +08:00
    xpresslink
        1
    xpresslink  
       Jun 22, 2018
    ?? 对于继承 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
       Jun 24, 2018
    @xpresslink 好的,非常感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2616 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 01:15 · PVG 09:15 · LAX 18:15 · JFK 21:15
    ♥ Do have faith in what you're doing.