自定义身份验证和Django登录问题

时间:2019-05-15 18:36:14

标签: python django pygithub

我的django自定义身份验证后端出现问题。对于一个学校项目,我需要通过GitHub进行身份验证,因此我使用了一个名为“ PyGithub”的软件包。我创建了自定义后端,但出现错误“无法在没有主键的情况下强制save()中进行更新”。当我调用login()方法时。

除了覆盖登录方法外,我真的不知道该怎么办,但不知道该怎么做。

这是我的自定义后端中的自定义身份验证功能:

def authenticate(self, request, username=None, password=None):
    try:
        g = Github(username, password)
        gitUser = g.get_user()
        user = User(username=username)
        return user
    except UnknownObjectException :
        return None
    return None

这是我的函数,它调用authenticate()和login():

def connexion(request):
    error = False
    if request.method == "POST":
        form = ConnexionForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            user = authenticate(username=username, password=password)
            if user:
                login(request, user)
            else:
                error = True
    else:
        form = ConnexionForm()
    return render(request, 'connexion.html', locals())

预先感谢!

0 个答案:

没有答案