【问题标题】:How to access user names and profiles with django-allauth如何使用 django-allauth 访问用户名和配置文件
【发布时间】:2012-01-18 15:16:51
【问题描述】:

我正在使用 Django 和 django-allauth 进行社交身份验证。

我已启动并运行身份验证,但谁能给出简单的示例说明如何:

  • 显示登录用户的姓名和头像
  • 向用户帐户添加一些信息?

例如,在主页上,我有

{% if user.is_authenticated %}
<li><a href="{% url account_logout %}?next=/">Logout</a></li>
{% endif %}

这样可以正确显示注销链接,但我该如何添加用户名和头像?

类似(伪代码):

<p>You're logged in with {{ user.account_provider? }} as {{ user }}.</p>
<img src="{{ user.avatar_url }}" />

然后,如果我想在用户的个人资料中添加额外的属性,我该怎么办?我应该使用其他一些与 Django 用户相关的应用程序吗?

感谢您的帮助。

【问题讨论】:

标签: python django oauth django-authentication


【解决方案1】:

所以我昨天也有库存,我已经在互联网上四处寻找解决方案,但没有一个看起来像我想出的那样可以理解...... 安装和配置 allauth 包后,我做了以下操作:

  1. 在app目录下创建signal.py文件

  2. 在你的 venv 中找到 allauth 包

  3. 然后导航到 allauth/socialaccount/models.py

  4. 这样你可以更好地理解如何从allauth包中抓取模型或调用其他函数和类

  5. 在 app/signal.py 中这样做:

     from django.db.models.signals import post_save;p0-
     from django.dispatch import receiver
     from .models import Profile
     from allauth.socialaccount.models import SocialAccount# step 3 made this possible
     from django.contrib.auth.models import User
    
     @receiver(post_save, sender = SocialAccount)
     def create_profile(sender, instance, created, **kwargs):
        if created:
           # Grabbing data from social account to create profile for that user
           profile=Profile(
                  user=instance.user,
                  photo=instance.extra_data['picture']
                          )
           profile.save()
         else:
        # Do something else
    

你看到信号的发送者是模型“SocialAccount”,因为这个模型总是在认证过程中创建,然后抓取字段“extra_data”,它是一个字典,然后是key“['picture']” “extra_data”字段中的 JSON 对象......我希望这对你有用。谢谢

这样您就可以获取图片的值并为用户分配个人资料

【讨论】:

    【解决方案2】:

    您可以在用户类的外键中的一组社交帐户中进行 for 循环,在模板中如下所示:

    {% for account in user.socialaccount_set.all %}
    
     {% comment %} show avatar from url {% endcomment %}
     <h2 style="text-transform:capitalize;">{{ account.provider }} account data</h2>
    
     <p><img width="50" height="50" src="{{ account.get_avatar_url }}"/></p>
    
     <p>UID: <a href="{{ account.extra_data.link }}">{{ account.uid }}</a></p>
    
     <p>Username: {{ account.extra_data.username }}</p>
    
      <p>First Name: {{ account.extra_data.first_name }}</p>
    
      <p>Last Name: {{ account.extra_data.last_name }}</p>
    
      <p>Dashboard Link: 
      <a href="{{ account.extra_data.link }}">{{ account.extra_data.link }}</a></p>
      {% empty %}
      <p>you haven't any social account please</p>
    {% endfor %}
    

    【讨论】:

      【解决方案3】:

      SocialAccount 模型实例可供使用社交帐户注册的用户使用。

      在你的模板中,你可以简单地写:

      头像网址: {{ user.socialaccount_set.all.0.get_avatar_url }}
      UID: {{ user.socialaccount_set.all.0.uid }}
      加入日期: {{ user.socialaccount_set.all.0.date_joined}}
      上次登录: {{ user.socialaccount_set.all.0.last_login}}

      对于全名: {{ user.socialaccount_set.all.0.extra_data.name }}

      欲了解更多信息:Django allauth source

      【讨论】:

      • 这是现代 Django (1.11.x) 和 allauth (0.32.x) 的正确答案。
      【解决方案4】:

      如果你查看 django-allauth 源码https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L7

      这是一个抽象模型,代表所有其他特定服务模型具有的所有方法。这样你就可以写

      <p>You're logged in with {{ user.get_provider }} as {{ user }}.</p>
      <img src="{{ user.get_avatar_url }}" />
      

      【讨论】:

      • 这些 get_provider() 和 get_avatar_url() 方法似乎没有通过最近的 django 和 django-allauth 传播到用户对象上。似乎这需要手动填充上下文。
      • SocialAccount 类仍然有这些方法。您确定上下文中有 SocialAccount 实例吗?
      • 我激活了 allauth.socialaccount.context_processors.socialaccount 上下文处理器,它在上下文中放置了一个“socialaccount”字典;此 dict 中的条目是活动提供者。不过,我看不到这个(或 SocialAccount 模型)如何修改用户对象,允许像“{{user.get_provider}}”这样的访问。也许我完全错过了你的观点?抱歉,我对 allauth、django 和 python 比较陌生,所以我可能遗漏了一些明显的东西。
      猜你喜欢
      • 2012-08-31
      • 2023-03-03
      • 2013-01-09
      • 2010-09-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多