python - 你有多个身份验证后端配置 Django?
问题描述
我有这个错误
You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
在 Django 的登录类基础视图中
登录视图
class SystemLoginView(LoginView):
template_name = "registration/login.html"
authentication_form = LoginForm
def post(self, request, *args, **kwargs):
username = request.POST['username']
password = request.POST['password']
db_name = request.POST['year']
branch = request.POST["branch"]
user = authenticate(username=username, password=password)
if user and user.active:
period_id = AccountingPeriod.objects.all()
if period_id:
request.session["period_id"] = period_id[0].id
else:
request.session["period_id"] = None
messages.error(
request,
_(
"Please You must Be initial the Period Before Any Operation"
),
)
set_defualt(
request.session.session_key,
int(branch),
)
request.session["branch_id"] = branch
cache.set("branch", branch)
list_branch = []
for x in user.user_branch.all():
dict_data = {"id": x.pk, "name": x.name}
list_branch.append(dict_data)
request.session["user_branch"] = list_branch
request.session["db_name"] = db_name
request.session["permission"] = get_all(request)
request.session["permission_partion"] = get_all_partion(request)
request.session["db_name"] = db_name
return super(SystemLoginView, self).post(request, *args, **kwargs)
登录表单类
class LoginForm(AuthenticationForm, IttechForm):
CHOICE = [(x, x) for x in DATABASES]
year = forms.ChoiceField(
choices=CHOICE,
label=_("Year"),
widget=forms.Select(attrs={"class": "form-control"})
)
branch = forms.ModelChoiceField(
queryset=Branch.objects.all(),
label=_("Branch"),
widget=forms.Select(attrs={"class": "form-control"})
)
username = forms.CharField(
label=_("Username"),
max_length=20,
widget=forms.TextInput(attrs={"placeholder": _("Username"), "class": "form-control"}),
)
password = forms.CharField(
label=_("password"),
max_length=30,
widget=forms.PasswordInput(attrs={"placeholder": _("Password"), "class": "form-control"}),
)
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
def clean(self):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
branch = self.cleaned_data.get("branch")
backend = ModelBackend()
user = backend.authenticate(self.request, username=username, password=password)
if not user:
raise self.get_invalid_login_error()
if not user.active:
self.confirm_login_allowed(user)
if branch:
if not user.user_branch.all().filter(id=branch.pk):
raise forms.ValidationError(
_("Sorry,This user can't login in this branch")
)
return self.cleaned_data
def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
"Sorry,This user was not active contact admin to solve this problem.",
code='inactive',
)
当用户经过身份验证并处于活动状态时,它会给我上面的错误。
请帮我解决这个问题。我已经尝试了很多互联网上的解决方案,但没有一个对我有帮助,而且我在我的 settings.py 文件中添加了 AUTHENTICATION_BACKENDS 但这不起作用。
解决方案
我已经解决了我的问题,只是在调用 super 之前添加了这行代码。
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
return super(SystemLoginView, self).post(request, *args, **kwargs)
一切正常。
推荐阅读
- powerbi - 如果满足条件,则获取两个日期之间的值
- reactjs - this.props.history.push 在不同的组件中给我未定义的错误
- php - Laravel 集合重命名嵌套键
- python - 使用 python 将 dict/JSON 转换为路径
- javascript - 是否可以在反应框架中悬停时添加 d3.js 饼图的工具提示
- php - 在 PHP 中,什么是反引号 (')?
- arrays - Numpy Unique 的运行/在线版本?
- html - 如何让背景色覆盖整个 div?
- python - Handling disconnects in Python ftplib FTP transfers file upload
- ssis - SSIS: How do I query for IDs from 1st database and then select data with only those IDs on 2nd database/data warehouse?