我如何从模板中的Django社交认证中获得特定的提供者? [英] How can i get a specific provider from Django social-auth in a template?
问题描述
在安装和设置社交认证后,我正在玩一下,试图弄清楚它。我已经阅读了文档,并且使用了示例项目来使其运行。
但到目前为止,我不知道如何获取某个提供商的信息。在示例项目中,模板标签总是以这种方式使用:
{%的类型,帐户在social_auth.associated.items%}
{%帐户中的帐户%}
{{account.provider}}已连接。
{%endfor%}
{%endfor%}
我想要什么现在做的不是列出所有的提供商,而是检查有人是否已经将他的帐户连接到(即。)Facebook。这样我可以这样做:
如果用户== connected_to_facebook
提供一些功能
endif
从上面的例子我知道social_auth.associated.items包含元素 (类型,帐户)
,其中facebook将在具有 account.provider
的所有值的列表中。
我想到的是:
{%如果facebook在social_auth。 associated.items.accounts.provider%}
显然,这不会工作。我认为这个将会奏效,但是没有返回我以后的结果:
{%如果facebook在social_auth。 associated.items [1] .provider%}
Django有一些功能,我可以做到用?也许有一些特殊的模板我失踪了?或者这个功能已经在social_auth中构建,我以某种方式错过了文档?或者,我最怀疑的是真的很明显,我只是...
任何帮助都非常受欢迎。
social_auth不是元组里面的一些元组,它是一个字典:
code'{'not_associated':{},'backends':{'oauth2':['facebook']},
'associated':{'oauth2':[< UserSocialAuth:testuser>]那么肯定会有更多的意义,但仍然不会在任何地方。所以我看了一个没有关联他的帐户的用户,那个字典看起来像这样:{' not'associated':{'oauth2':['facebook']},'backends':{'oauth2':['facebook']},
'associated':{}}
现在我发现有用的东西:
{%iffacebookin social_auth.not_associated.oauth2%}
{%else%}
提供Facebook功能
{%endif%}
这样做。所有你需要知道的是你正在寻找的后端的身份验证类型,然后确保它不在social_auth的not_associated字段中。
After installing and setting up social-auth i'm playing around a bit, trying to get the hang of it. I have read the docs, and have used the example project to get it running.
But until now i have no idea how to get information on a certain provider. In the example project the templatetags are always used this way:
{% for type, accounts in social_auth.associated.items %} {% for account in accounts %} {{account.provider}} is connected. {% endfor %} {% endfor %}
What i want to do now is not list all the providers, but check if someone has connected his account to (ie.) facebook. That way i could do something like this:
if user==connected_to_facebook provide some functionality endif
From the example above i know that social_auth.associated.items contains tuples of
(type,account)
, where "facebook" would then be in a list with all values foraccount.provider
.What comes to my mind is this:
{% if "facebook" in social_auth.associated.items.accounts.provider %}
which, obviously, is not going to work. I think this one will work, but not return the result i'm after:
{% if "facebook" in social_auth.associated.items[1].provider %}
Is there some functionality in Django that i can do this with? Maybe some special templatetag i'm missing? Or is this functionality already built into social_auth, and i somehow missed the documentation? Or, my worst suspicion, is it really really obvious, and i'm just ...
Any help extremely welcome.
解决方案"social_auth" is not some tuples inside of tuples, it is a dictionairy:
{'not_associated': {}, 'backends': {'oauth2': ['facebook']}, 'associated': {'oauth2': [<UserSocialAuth:testuser>]}}
That certainly makes a lot more sense, but still does not lead anywhere. So i took a look at a user that has not associated his account yet, and there that dictionary looks like this:
{'not_associated': {'oauth2': ['facebook']}, 'backends': {'oauth2': ['facebook']}, 'associated': {}}
Now i found something usefull:
{% if "facebook" in social_auth.not_associated.oauth2 %} {% else %} provide facebook functionality {% endif %}
That works. All you have to know is what type of authentication the backend you are looking for uses, then make sure it is not in the not_associated field of social_auth.
这篇关于我如何从模板中的Django社交认证中获得特定的提供者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!