简体   繁体   English

检查用户是否使用社交帐户登录 - Django

[英]check if the user has logged in with social account - Django

in the web site that i'm working on, I have an authetication system and I'm using django allauth at the same time so the user can login with his gmail or facebook account if he wants.在我正在使用的 web 站点中,我有一个身份验证系统,我同时使用 django allauth,因此用户可以根据需要使用他的 gmail 或 facebook 帐户登录。 how can I check in the template if the user has logged in with his facebook/gmail account?如果用户已使用他的 facebook/gmail 帐户登录,我如何签入模板? i want somthing like this我想要这样的东西

{% if user not 'use his facebook or gmail'  %}                     
    <a href="{% some url %}">
       <button type="button" class="btn btn-primary" data-dismiss="modal">somthing </button>
      </a> 
{% endif %}

for future readers, in view.py you can something like this to detect if user is a social user or not对于未来的读者,在view.py中你可以这样来检测用户是否是社交用户

from allauth.socialaccount.models import SocialAccount

is_social = SocialAccount.objects.filter(user=request.user).exists()

it returns True if your logged in with facebook/google/... else it return False如果您使用facebook/google/...登录,则返回True ,否则返回False

If you just want to check that they are logged in, you can use the following:如果您只想检查他们是否已登录,可以使用以下命令:

{% if user.is_authenticated %}
    <a href="{% some url %}">
       <button type="button" class="btn btn-primary" data-dismiss="modal">somthing </button>
      </a> 
{% endif %}

If you want to see that they are logged in via one of these two specific platforms, you'll probably want to do that in the view instead of the template.如果您想查看他们是通过这两个特定平台之一登录的,您可能希望在视图而不是模板中执行此操作。

i've found this and it has done what I needed我找到了这个,它完成了我需要的

 {% load socialaccount %}
    
  {% get_social_accounts user as accounts %}
  {% if accounts %} "if the user has logged in using his facebook account or his google account"

          "do somthing here"
        
  {% endif %}

for more details check the documentation of django-allauth有关更多详细信息,请查看 django-allauth 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM