如何为Django自定义用户创建登录视图? [英] How to make a login view for django custom user?
问题描述
我已经使用AbstractUser制作了一个自定义用户模型,删除了用户名并用电子邮件替换了它,并扩展了该模型,尝试创建超级用户,它可以工作,并且还通过注册表单创建了一些用户,并登录了管理界面,可以,但是当尝试为用户创建登录表单时,它失败了
I have made a custom user model using the AbstractUser , removed the username and replaced it with email and extended the model, tried creating superuser and it worked and also created some users by a signup form , logged in the admin interface and it worked however when tried to create a login form for the users it fails
我尝试了一下,但是没有用
I tried this but it didn't work
def LoginView(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = form.get_user()
login(request,user)
return redirect('accounts:login')
else:
form = AuthenticationForm()
return render(request,'accounts/login.html', {'form':form})
然后我尝试了
class LoginView(FormView):
form_class = AuthenticationForm
template_name = 'login.html'
def form_valid(self, form):
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(email=email, password=password)
# Check here if the user is an admin
if user is not None and user.is_active:
login(self.request, user)
return HttpResponseRedirect(self.success_url)
else:
return self.form_invalid(form)
显然,我希望用户登录
i认为此帖子中的代码格式错误。主要是我的错,因为我是这个平台的新手
Obviously i expect the user to be logged in i think the code in this post is badly formatted. mainly it's my fault as i'm new to this platform
推荐答案
我开发了几乎与您描述的相同的设置(我没有删除用户名字段,只是停止使用它)。如果您还没有看到它,请参阅 https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#substituting-a-custom-user-model 很有帮助。
I've developed almost the same setup that you're describing (I didn't remove the username field, just stopped using it). If you haven't already seen it, Django's documentation at https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#substituting-a-custom-user-model is quite helpful.
有一些重要的事情需要正确设置才能使它起作用。
There are a couple of important things that need to be set up correctly for this to work.
- 模型上的
USERNAME_FIELD
应该设置为电子邮件字段的名称。 -
AUTH_USER_MODEL
需要指向您的自定义用户模型。
- The
USERNAME_FIELD
on your model should be set to the name of your email field. - The
AUTH_USER_MODEL
needs to point to your custom user model.
class MyUser(AbstractUser):
USERNAME_FIELD = 'email'
AUTH_USER_MODEL = 'customauth.MyUser'
由于完全删除了用户名字段,因此可能需要将 django.contrib.auth.forms.AuthenticationForm
和 django子类化。 contrib.auth.views.LoginView
以避免破坏内容,但是Django应该很好地处理其他身份验证字段。
Since you've removed the username field altogether you might need to subclass django.contrib.auth.forms.AuthenticationForm
and django.contrib.auth.views.LoginView
to avoid breaking things, but Django should handle a different authentication field quite well.
需要子类化视图, https://ccbv.co.uk/projects/Django/2.2/django.contrib.auth.views/LoginView/ 是查看所有方法的好地方,以查看发生了什么。
If you do wind up needing to subclass the view, https://ccbv.co.uk/projects/Django/2.2/django.contrib.auth.views/LoginView/ is a great place to look over all the methods to see what's going on.
编辑-关于子类化,这是必要的
我所说的可能需要对某些事物进行子类化是受 https://docs.djangoproject.com/en/2.2/topics/auth/为一个自定义用户模型自定义/#manager 。我不确定身份验证系统是否还有其他部分需要您自定义,因为您删除了用户名字段。
What I was saying about possibly needing to subclass certain things was influenced by https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#writing-a-manager-for-a-custom-user-model. I wasn't sure if there were other parts of the authentication system that would need you to customize them because you removed the username field.
我已经阅读了一些Django身份验证系统的源代码。
I've read through some of the source code for Django's authentication system. Here's the path that's being followed.
-
对Django的身份验证视图发出POST请求时,身份验证表单便得到了验证。 https://github.com/ django / django / blob / 2.2.2 / django / contrib / auth / forms.py#L191
调用了authenticate函数。这会遍历设置的后端,并尝试对每个后端进行身份验证。 https://github.com/ django / django / blob / 2.2.2 / django / contrib / auth / __ init __。py#L62
The authenticate function is called. This iterates through the backends set up and tries to authenticate on each of them. https://github.com/django/django/blob/2.2.2/django/contrib/auth/__init__.py#L62
Django的内置身份验证后端用户是否使用自然键存在。 https://github.com/ django / django / blob / 2.2.2 / django / contrib / auth / backends.py#L16
Django's built-in authentication backend gets the user if it exists using the natural key. https://github.com/django/django/blob/2.2.2/django/contrib/auth/backends.py#L16
我们可以在基本管理器中看到所使用的自然键是由 USERNAME_FIELD
命名的字段。 https://github.com/ django / django / blob / 2.2.2 / django / contrib / auth / base_user.py#L43
We can see in the base manager that the natural key used is the field named by USERNAME_FIELD
. https://github.com/django/django/blob/2.2.2/django/contrib/auth/base_user.py#L43
如果该格式有效,则表示用户正确身份验证,然后登录用户。 https://github.com/django/django/blob/2.2.2/django/contrib/auth/views.py#L88
If the form is valid, meaning that the user is authenticated properly, the user is then logged in. https://github.com/django/django/blob/2.2.2/django/contrib/auth/views.py#L88
我的反应是,Django应该适合您的用例。您不需要编写后端。这是我的直觉告诉你必须编写的代码范围。
My reaction is that it looks like Django should work out of the box for your use case. You shouldn't need to write a backend. Here's the extent of the code my gut says you should have to write.
from django.contrib.auth import views as auth_views
from django.shortcuts import resolve_url
class LoginView(auth_views.LoginView):
template_name = 'accounts/login.html'
def get_success_url(self):
return resolve_url('accounts:login')
这篇关于如何为Django自定义用户创建登录视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!