gpt4 book ai didi

python - 无法从登录页面进一步移动,[django-otp]

转载 作者:行者123 更新时间:2023-11-28 18:40:39 24 4
gpt4 key购买 nike

问题:

在提供用户名和密码后,我重定向回登录页面并显示以下错误消息

“请输入正确的电子邮件和密码。请注意,这两个字段可能区分大小写。”

详细信息:

我已遵循 Django Two-Factor Authentication 的文档.我一定在配置中遗漏了一些东西,但不确切知道。以下是我的代码中的详细信息。

settings.py

# in middle ware classes
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware',
...

# in installed apps
...
'django_otp',
'django_otp.plugins.otp_totp',
'django_otp.plugins.otp_hotp',
'django_otp.plugins.otp_static',
'two_factor',
'otp_yubikey',
...

LOGIN_URL = reverse_lazy('two_factor:login')

TWO_FACTOR_PATCH_ADMIN = True
TWO_FACTOR_CALL_GATEWAY = 'two_factor.gateways.fake.fake'
TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.fake.Fake'
TWO_FACTOR_QR_FACTORY = 'qrcode.image.pil.PilImage'
LOGIN_REDIRECT_URL = reverse_lazy('two_factor:profile')
AUTHENTICATION_BACKENDS = ('myapp.backends.EmailAuthBackend',)
AUTH_USER_MODEL = 'users.User'
OTP_LOGIN_URL = LOGIN_URL

Logger 看起来也不错

urls.py

url(r'', include(tf_urls + tf_twilio_urls, 'two_factor')),

并在views.py

@otp_required()
def home(request):
# some logic with HTTP response

注意: 我在我的应用程序中有自定义用户模型,并在 admin.py 中注册了它们,除了这个问题之外,我也粘贴了该文件以便清楚。

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from apps.users.models import User
from django import forms


class MyUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = User


class MyUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User

def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])


class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
# fieldsets = UserAdmin.fieldsets + (
# (None, {'fields': ('extra_field1', 'extra_field2',)}),
# )

admin.site.register(User, MyUserAdmin)

请指出我哪里出错了。如果我不太清楚,请告诉我。谢谢!

最佳答案

好的,我的观点是:由于您使用从用户模型继承,您需要使用 AUTH_USER_MODEL 作为授权路由。这意味着,您已经有两种身份验证方式。也许您应该检查两个表:otp_device 和 auth_users。我的意思是,当您将日志/密码放入表单时,它会检查 auth_users 表。但是在设备表中你没有请求用户。因此,otp 身份验证系统无法匹配 None 记录并在登录页面上重定向您。

如果我有类似的情况,我决定尝试两种解决方案:

  1. 创建信号以同步用户和设备表;
  2. 使用旧版本的用户自定义(带有用户模型的 OneToOneField)。

我认为这种授权方式太复杂了:三个应用程序(two_factor、otp 和默认授权)都在使用类似的授权技术。

关于python - 无法从登录页面进一步移动,[django-otp],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26609517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com