如何使用 django-allauth 访问用户名和配置文件

发布于 2024-12-21 01:39:24 字数 603 浏览 4 评论 0原文

我使用 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 用户相关的应用程序吗?

感谢您的帮助。

I'm using Django with django-allauth for social authentication.

I have authentication up and running, but can anyone give simple examples of how to:

  • show the name and avatar of a logged-in user
  • add some information to a user's account?

For example, on the home page, I've got

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

That's showing the Logout link correctly, but how would I add the user's name and avatar?

Something like (pseudocode):

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

Then, if I want to add extra properties to the user's profile, what do I do? Should I be using some other Django user-related app?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ˉ厌 2024-12-28 01:39:24

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 源

A SocialAccount model instance is available for users who signed up using their social account.

In your template, you can simply write:

Avatar URL: {{ user.socialaccount_set.all.0.get_avatar_url }}
UID: {{ user.socialaccount_set.all.0.uid }}
Date Joined: {{ user.socialaccount_set.all.0.date_joined}}
Last Login: {{ user.socialaccount_set.all.0.last_login}}

And for Full Name: {{ user.socialaccount_set.all.0.extra_data.name }}

For more information: Django allauth source

撑一把青伞 2024-12-28 01:39:24

如果您查看 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 }}" />

If you look at django-allauth source https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L7

This is an abstract model that represents all the methods all other specific service models have. Thus you could write

<p>You're logged in with {{ user.get_provider }} as {{ user }}.</p>
<img src="{{ user.get_avatar_url }}" />
南汐寒笙箫 2024-12-28 01:39:24

您可以在外键到用户类的社交帐户集中进行 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 %}

you can make for loop in set of socialaccount within foreignkey to user class, in the template it's something like below :

{% 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 %}
救星 2024-12-28 01:39:24

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

  1. 在应用程序目录中创建一个 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-
     从 django.dispatch 导入接收器
     从 .models 导入配置文件
     from allauth.socialaccount.models import SocialAccount# 第 3 步使这成为可能
     从 django.contrib.auth.models 导入用户
    
     @receiver(post_save, 发件人 = SocialAccount)
     def create_profile(发件人,实例,创建,**kwargs):
        如果创建:
           # 从社交帐户中获取数据来为该用户创建个人资料
           个人资料=个人资料(
                  用户=实例.用户,
                  照片=instance.extra_data['图片']
                          )
           配置文件.save()
         别的:
        # 做点别的事
    

您会看到信号的发送者是模型“SocialAccount”,因为该模型始终在身份验证期间创建,然后获取字段“extra_data”(它是一个字典),然后获取键“[ 'picture']”来自“extra_data”字段中的 JSON 对象...我希望这对您有用。谢谢

这样您就可以获取图片的值并为用户分配配置文件

So i got stock here too yesterday, i have gone round the internet for solution but none was looking as understandable as what i came up with...
After installation and configuration of the allauth package, i did the following:

  1. create a signal.py file in app directory

  2. Inside your venv locate the allauth package

  3. Then navigate to allauth/socialaccount/models.py

  4. This way you can have a better understanding on how to grab models from the allauth package or call other functions and classes

  5. In app/signal.py do this:

     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
    

You see that the sender of the signal is the model "SocialAccount", because this model is always created during authentication, then grab the field "extra_data" which is a dictionary and then the key "['picture']" from the JSON object in the field "extra_data"... I hope this works for you. Thank you

This way you are able to grab the values of the picture and also assign the user with a profile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文