我想创建一个使用 django admin 的应用程序,但允许通过 google(我公司的 google 帐户)代替 django 默认 ModelAdmin 登录。
目前,它看起来像social-app-django (google) 是要走的路,但是在安装和设置项目之后,我不清楚如何允许 django 管理员登录使用 social-app-django验证。我已尝试按照此处所述配置我的项目 http://python-social-auth.readthedocs.io/en/latest/configuration/django.html但尚不清楚如何将其与 django 管理集成。
我找到了这个 snippit (这似乎过时),并添加了它,但是当我尝试转到 /admin/ 时得到 404:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/accounts/login/?next=/admin/login/%3Fnext%3D/admin/ Using the URLconf defined in telos.urls, Django tried these URL patterns, in this order: ^login/(?P[^/]+)/$ [name='begin'] ^complete/(?P[^/]+)/$ [name='complete'] ^disconnect/(?P[^/]+)/$ [name='disconnect'] ^disconnect/(?P[^/]+)/(?P[^/]+)/$ [name='disconnect_individual'] ^admin/ The current path, accounts/login/, didn't match any of these.
如果我删除代码片段,/admin/ 将重定向到 /admin/login/ 并在尝试登录时返回错误文本:
Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.
除了configuration ,我已将以下内容添加到我的 settings.py:
projects/models.py(我的用户)
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
pass
settings.py
# for identification of SOCIAL_AUTH_USER
# http://python-social-auth.readthedocs.io/en/latest/configuration/settings.html#user-model
SOCIAL_AUTH_USER_MODEL = 'projects.MyUser'
AUTH_USER_MODEL = 'projects.MyUser' # not sure if this is needed
任何人都可以指导我如何设置我的项目以允许我通过 social-app-django 登录到 django 管理员吗? (谷歌)?
请您参考如下方法:
Django Admin 使用 auth contrib 应用程序,因此任何身份验证过程都会触发与用户登录到非管理部分相同的机制,它将由 python-social- 处理auth 后端,如果它们在 AUTHENTICATION_BACKENDS 设置中定义的话。
为了使其正常工作,您需要:
- 将
Login with Google链接(链接到/login/google-oauth2)添加到登录表单。您可以通过添加 admin/login.html 来覆盖默认登录表单模板或通过定义自定义 AdminSite - 确保用户被标记为
is_staff,否则将禁止访问管理员。
