首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从social-auth-app-django访问用户数据

无法从social-auth-app-django访问用户数据
EN

Stack Overflow用户
提问于 2021-05-10 14:32:23
回答 1查看 61关注 0票数 0

我被Django的身份验证弄得不知所措,并试图在一段时间内理解它。我使用了OAuth -auth-app-Django来获得GitHub和Facebook的授权。使用GitHub登录后,我检查了管理页面,GitHub工作了,脸书不工作,

在user social auths部分,我无法使用html页面中的模板语法访问提供者名称,没有显示出来!

有人能解释一下这里到底发生了什么吗?什么是关联和随机数?

这是我的html。

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 </head>
 <body>
 {%if user.is_authenticated%}
 <h1>{{user.provider}}</h1>
 <h1>{{user.username}}</h1>



{%else%}
<h1>you are not logged in</h1>
{%endif%}
</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2021-09-07 13:00:47

您可以通过执行以下操作来访问provider:

在views.py中

代码语言:javascript
复制
def get_provider(request):
    user= request.user
    user.social_auth.get(provider='provider name')
    provs= ['github','twitter','facebook','google-oauth2'] # Any backend in the social auth back end 
    found=list()
    notfound=list()

    for prov in provs :
    try:
        user.social_auth.get(provider=prov)
        print("Found ",prov)
        found.append(prov)
    except:
        print(prov,"Not found ")
        notfound.append(prov)
    
    return render(request, 'registration/social_login.html', {
    'found':found,
    'not_found':notfound,
    })     

然后在你的模板中

social_login.html

代码语言:javascript
复制
{% for element in found %}
<p>you are auth as {{element}}</p>
{% endfor %}
{% for el in not_found %}
<p>you are not auth in {{el}}</p>
{% endfor %}

您可以在这里找到更多信息https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67465759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档