如上所述,我想使用django" authenticate"使用一个参数作为电子邮件的方法因为我使用mastodon api来验证用户并且我已经从mastodon获得了访问令牌。在此之后,我只想在任何django方法的帮助下检查django用户表中是否存在该电子邮件。我使用了authenticate方法,但在密码重置的用例中失败了。 django中是否有任何方法只能通过电子邮件对用户进行身份验证。我在下面提到我的代码,如果代码中的任何修改使得代码运行会很明显。
Django代码:
mastodon_var = mastodon.Mastodon(
client_id='gnowsys_ndf/ndf/NROER-client_cred.secret',
api_base_url='https://member.metastudio.org'
)
access_token = None
####GET ACCESS FROM MASTODON HERE#######
try:
access_token = mastodon_var.log_in(
Username,
Password,
to_file='gnowsys_ndf/ndf/NROER-access_token.secret',
)
except Exception as e:
print e
pass
name = Username
email = Username
password = Password
# userna = mastodon_var.u"username"
# print userna
#######IF USER GETS ACCESS TOKEN THEN FOLLOWING CODE WILL BE EXECUTED##########
if access_token:
if User.objects.filter(username=name).exists():
####SECOND AND BY-DEFAULT LAYER FOR AUTHENTICATION
user = authenticate(username=name,password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect( reverse('landing_page') )
else:
HttpResponse("Error1")
return HttpResponseRedirect( reverse('landing_page') )
else:
member = User.objects.create_user(name,email,password)
member.save()
####SECOND AND BY-DEFAULT LAYER FOR AUTHENTICATION
user = authenticate(username=name, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect( reverse('landing_page') )
else:
HttpResponse("Error2")
return HttpResponseRedirect( reverse('landing_page') )
else:
#t = TemplateResponse(request, 'login_template', {})
#return t.render()
return HttpResponse("OOps!!!!! You entered wrong credentials")
#return HttpResponseRedirect( reverse('landing_page') )
else:
return HttpResponse("Invalid Credentials.")