私は、ユーザーがis_authenticated=True
とマークされているにもかかわらず、authenticate
がバックエンドを割り当てられないため、ログインに失敗した登録フォームを作成しました。私は認証を使用している方法で間違っていますか? (私はここに影響を与えているかどうかわからジャンゴall_authを使用しますがないよに注意?)Django - 認証はユーザーにバックエンドを追加しません
ログイン時()、それは、このエラーを生成:
ValueError:You have multiple authentication backends configured and therefore must provide the backend argument or set the backend attribute on the user.
ビュー:
....
form = ProfileForm(request.POST)
if form.is_valid():
user_profile, user = form.save()
authenticate(request, user=form.cleaned_data['email'],
password=form.cleaned_data['password1'])
login(request, user)
models.py
class User(AbstractUser):
def __str__(self):
return self.username
def get_absolute_url(self):
return reverse('users:detail', kwargs={'username': self.username})
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
TITLE_CHOICES = (
.....
)
title = models.CharField(max_length=5, null=True, choices=TITLE_CHOICES)
date_of_birth = models.DateField()
primary_phone = PhoneNumberField()
EMPLOYMENT_CHOICES = (
(....,...)
)
employment_status = models.CharField(max_length=35, choices=EMPLOYMENT_CHOICES)
プロフィールの書式:
class ProfileForm(allauthforms.SignupForm):
title = FieldBuilder(UserProfile, 'title',)
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
date_of_birth = FieldBuilder(UserProfile, 'date_of_birth', widget=SelectDateWidget())
primary_phone = FieldBuilder(UserProfile, 'primary_phone')
employment_status = FieldBuilder(UserProfile, 'employment_status')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["employment_status"].choices = [("", "---- Select your Employment Status ----"), ] + list(
self.fields["employment_status"].choices)[1:]
def save(self, *args):
data = self.cleaned_data
user = User.objects.create_user(
username=data['email'],
email=data['email'],
password=data['password1'],
first_name=data['first_name'],
last_name=data['last_name'],
)
instance = UserProfile.objects.create(
user=user,
date_of_birth=data['date_of_birth'],
primary_phone=data['primary_phone'],
title=data['title'],
employment_status=data['employment_status'],
)
return instance, user
あなたの表示コードは非常に混乱しています。 'user'は' authenticate'からの戻り値であり、それに対するパラメータではありません。 –