- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基于类的 View ,子类 LoginView
.
from django.contrib.auth.views import LoginView
class CustomLoginView(LoginView):
def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})
Please enter a correct email address and password. Note that both fields may be case-sensitive.
Please confirm your email so you can log in.
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import gettext as _
class PickyAuthenticationForm(AuthenticationForm):
def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
_("Please confirm your email so you can log in."),
code='inactive',
)
class CustomLoginView(LoginView): # 1. <--- note: this is a class-based view
form_class = PickyAuthenticationForm # 2. <--- note: define form here?
def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})
最佳答案
方法 - 1
Django 使用 ModelBackend
默认 AUTHENTICATION_BACKENDS
并且不验证非事件用户。
Authorization for inactive users 中也说明了这一点。部分,
An inactive user is one that has its
is_active
field set toFalse
. TheModelBackend
andRemoteUserBackend
authentication backends prohibitsthese users from authenticating. If a custom user model doesn’t haveanis_active
field, all users will be allowed to authenticate.
AllowAllUsersModelBackend
作为您的
AUTHENTICATION_BACKENDS
在
settings.py
# settings.py
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']
AllowAllUsersModelBackend
class我们可以看到它只允许
非事件 用户进行身份验证。
clean(...)
的方法| PickyAuthenticationForm
上课并调用
AllowAllUsersModelBackend
后端为,
from django.contrib.auth.backends import AllowAllUsersModelBackend
class PickyAuthenticationForm(AuthenticationForm):
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if username is not None and password:
backend = AllowAllUsersModelBackend()
self.user_cache = backend.authenticate(self.request, username=username, password=password)
if self.user_cache is None:
raise self.get_invalid_login_error()
else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data
def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
"Please confirm your email so you can log in.",
code='inactive',
)
结果截图
关于django - 使用自定义 AuthenticationForm 覆盖 Django LoginView 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61470820/
我正在尝试覆盖 Django 应用程序中的属性,但由于某种原因,我无法覆盖 LoginView 的某些属性。我尝试将其作为我自己的基于类的 View 和直接在 urlconfig 上执行,将属性作为参
高级问题:我可以使用 success_url属性更改为从 Django 继承的 View 的位置 LoginView会重定向吗? 我有一个类似于下面的登录 View (忽略导入语句): class M
我试图弄清楚如何根据用户当天是否第一次登录来自定义 django LoginView。我当前已设置 LoginView,使其默认为 settings.py 文件中的 LOGIN_REDIRECT_UR
所以我正在使用 LoginView 控件。这就是我所知道的,如果用户登录,它将显示模板的登录 View ,否则显示 AnonymousTemplate。这在我的页面上有效。我在多个页面上使用过它。 现
我正在使用 asp.net LoginView 向经过身份验证的用户或匿名用户显示不同的数据。 当我将 Foo 和 Ba
很抱歉,如果这个问题已经被问过,但我没有找到答案。 我是 ios 开发新手,我已经有一个带有 LoginActivity 的 Android 应用程序,当登录成功时,该应用程序会重定向到 Naviga
我的页面上有这个登录 View 。如何从代码隐藏切换角色组? 说...我目前在爸爸登录 View 模板,我想更改为管理员登录 View 模板: I Am Admin
在 Visual Studio 中创建 ASP.NET 项目时,默认网站模板会显示用户在登录 View 中登录时的用户名。我想用用户的名字替换它。我将其存储在数据库中,因此在 Site.master
如何在登录系统时将状态从第一个模板更改为第二个模板? [ Log In ] Welcome! [ ] 如何
我做了一个自定义的 loginView,但在我的模板中无法访问 extra_context 字典。 (认证工作正常) 我的 View 文件: from django.contrib.auth impo
我正在尝试在 Django 2 中自定义身份验证和 View ,但问题是,如果用户已经通过身份验证,则仍会显示登录表单,并且不会重定向到适当的 URL。为了克服这个问题,我做了以下工作: class
我在 Django 中使用 LoginView 类。我们可以使用此类作为带有 HTML 模板的登录 View ,如下所示: 网址: from django.contrib.auth.views imp
我试图在 LoginView LoggedIn 模板中获取 LoginName 控件,尽管有几种方法,代码仍然无法运行。 我希望将默认用户名更改为客户名称。 代码如下:
我有一个 LoginView,它基本上是从面包店入门项目中复制的。 当我运行面包店应用程序并尝试使用错误的凭据登录时,会出现一个带有错误消息的附加 div。 但是,此错误消息 div 没有出现在我的
我正在尝试在我的应用程序中添加一个 FBLoginView,如 here 所述. FBLoginView *loginView = [[FBLoginView alloc] init]; [self.
在我的 PFLogInViewController 子类的 CustomLogInViewController 类中,我有这样的代码: @implementation CustomLogInViewC
我有一个登录 View ,可以通过 SWRevealController 的滑出菜单切换到其他 View 。 Storyboard 如下所示:http://imgur.com/a/Qx3Tj 我在 l
另一个全新的问题,恐怕:我有一个 LoginView,里面有一些 HyperLinks,但是当我尝试在它背后的代码中引用 HyperLink 时告诉我它在“当前上下文”中不存在。 例如。 hypLin
我尝试将 django.contrib.auth.LoginView 和 auth_views.LogoutView.as_view() 与自定义模板一起使用,而不以任何方式干扰管理站点,无论应用程序
我正在尝试使用 bootstrap 提供的模板,但想在导航栏的右侧添加登录 View 和登录状态。我无法正确修改它,所以我可以包含登录状态。请帮忙这是使用 bootstrap 的母版页
我是一名优秀的程序员,十分优秀!