2017-04-06 39 views
2

我想創建一個使用django admin的應用程序,但允許通過谷歌(我的公司谷歌帳戶)登錄來代替django默認ModelAdmin。集成與Django admin一起使用的python-social-auth(social-app-django)

目前,它看起來像social-app-django谷歌)是的路要走,但是已經安裝並設置一個項目後,這是我不清楚我怎麼才能讓Django管理登錄使用social-app-django認證。我試圖配置我的項目,如http://python-social-auth.readthedocs.io/en/latest/configuration/django.html所述,但不清楚如何將其與django管理員集成。

我發現這個snippit(這似乎外的日期),並補充道,卻得到了一個404,當我嘗試轉到/admin/:找不到

頁(404)請求方法: GET請求 URL:http://127.0.0.1:8000/accounts/login/?next=/admin/login/%3Fnext%3D/admin/ 使用在telos.urls中定義的URLconf,Django按以下順序嘗試了這些URL 模式:^ login /(?P [^ /] +)/ $ [name ='begin'] ^完成/(?P [^ /] +)/ $ [name ='complete'] ^ disconnect /(?P [^ /] +)/ $ [name ='disconnect'] ^ di sconnect /(?P [^ /] +)/(?P [^ /] +)/ $ [name ='disconnect_individual']^admin /當前路徑, accounts/login /,這些。

如果我刪除的這段,/admin/會重定向到/admin/login/及登錄嘗試返回錯誤文本:

請輸入員工帳戶正確的用戶名和密碼。 請注意,這兩個字段可能區分大小寫。

除了configuration,我已經添加了以下到我的settings.py

項目/ models.py(MYUSER)

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 

Ca ñ任何人指導我如何設置我的項目,讓我登錄到Django管理通過social-app-django谷歌)?

回答

1

Django管理使用auth的contrib應用程序,所以任何認證過程觸發比用戶登錄相同的機制中,以非管理部分,它將通過python-social-auth後端如果它們在AUTHENTICATION_BACKENDS設置中定義進行處理。

爲了使其工作,你將需要:

  1. 添加Login with Google鏈接(鏈接到/login/google-oauth2)的登錄表單。您可以通過添加admin/login.html模板或通過定義自定義來覆蓋默認登錄表單AdminSite
  2. 確保用戶被標記爲is_staff,否則將禁止訪問管理員。
+0

必須弄清楚如何覆蓋模板等等,但得到它的工作。謝謝! – monkut