Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 身份验证后如何在django allauth中获取GitHub用户名?_Python_Django_Python 3.x_Github_Django Allauth - Fatal编程技术网

Python 身份验证后如何在django allauth中获取GitHub用户名?

Python 身份验证后如何在django allauth中获取GitHub用户名?,python,django,python-3.x,github,django-allauth,Python,Django,Python 3.x,Github,Django Allauth,我在django项目中添加了django allauth,我可以在网站上使用GitHub进行身份验证。 用户身份验证后,我想获取GitHub用户名(登录的用户)。 如何操作?{%for user.socialaccount\u set.all%} {% for account in user.socialaccount_set.all %} <p>Username: <a target="_blank" href="{{ acc

我在django项目中添加了
django allauth
,我可以在网站上使用GitHub进行身份验证。
用户身份验证后,我想获取GitHub用户名(登录的用户)。
如何操作?

{%for user.socialaccount\u set.all%}
{% for account in user.socialaccount_set.all %}

    <p>Username: <a target="_blank"
                    href="{{ account.extra_data.html_url }}">{{ account.extra_data.login }}</a>
    </p>

{% endfor %}
用户名:

{%endfor%}

extra\u data.login
-显示GitHub的登录。

allauth设置的用户名与从GitHub返回的用户名不同,它是从函数
generate\u unique\u username
中返回的值,请查看它,因此您最好访问SocialAccount的extra\u数据字段,下面是显示如何访问它的示例视图

from django.shortcuts import HttpResponse, render
from allauth.socialaccount.models import SocialAccount
def home(request):
    if request.user.is_authenticated:
        try:
            social_account=SocialAccount.objects.get(user=request.user).extra_data
            return HttpResponse(social_account['login'])
        except SocialAccount.DoesNotExist: # user created with email and password
            return HttpResponse(request.user.username)
    return HttpResponse("<a href='/accounts/github/login/'>Sign Up</a>")
从django.shortcuts导入HttpResponse,呈现
从allauth.socialaccount.models导入socialaccount
def home(请求):
如果request.user.u经过身份验证:
尝试:
社交帐户=社交帐户.objects.get(用户=请求.user).extra\u数据
返回HttpResponse(社交_帐户['login'])
除了SocialAccount.DoesNotExist:#使用电子邮件和密码创建的用户
返回HttpResponse(request.user.username)
返回HttpResponse(“”)