【发布时间】:2018-12-01 12:55:36
【问题描述】:
我正在创建一个网站,银行使用其用户名登录,该用户名是一个代码,但我希望该银行可以使用其名字登录。
我正在使用默认用户模型进行注册。
但authenticate() 函数仅适用于用户名,所以我想做的是银行填写他们的名称,函数在数据库中找到具有对应名称的用户名值,然后使用authenticate() 函数登录银行。
我在 view.py 中的登录功能
def login(request):
if request.method == 'POST':
name = request.POST.get('first_name')
password = request.POST.get('password')
username = ????????
user = authenticate(username=username, password=password)
if user:
if user.is_active and has_role(user,Banker):
auth_login(request,user)
return HttpResponseRedirect(reverse('business:dashboard'))
else:
messages.error(request,"Your account is not active")
return render(request,'accounts/bank_login.html')
else:
messages.error(request,"Invalid Username or Password")
return render(request,'accounts/bank_login.html')
else:
if request.user.is_authenticated:
return HttpResponseRedirect(reverse('business:dashboard'))
else:
return render(request,'accounts/bank_login.html')
所以请任何人都可以告诉我应该在该用户名中写什么以从数据库中获取用户名的值
**我的模型.py **
from django.db import models
from django.contrib import auth
# Create your models here.
class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
return self.user.username
【问题讨论】:
标签: python-3.x sqlite django-models django-forms django-views