django 无法在自定义用户模型中登录

vwhgwdsa  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(151)

我有自定义的用户模型,工程注册和创建超级用户的商品。但登录失败。我一直试图纠正这个问题,但找不到任何潜在的错误...
登录表单没有登录用户,当我提交表单时,它只是说该用户名的用户已经存在(这是一种像注册页面的行为)如何解决这个问题,否则我会得到一个'页面无法访问联系网站所有者'

models.py

class Organisation(models.Model):
    organisation_name =  models.CharField(max_length = 256)
    contact_no = models.IntegerField()
    email = models.EmailField()

    class Meta():
        unique_together = ['organisation_name','email']

    def  __str__(self):
        return self.organisation_name

class MyUserManager(BaseUserManager):
    def create_user(self, username, organisation, email, password):
        if not organisation:
            raise ValueError("Users must have an organisation")
        email = self.normalize_email(email)
        user = self.model(username=username, email=email, organisation=organisation)
        user.set_password(password)
        user.save(using=self.db)
        return user
    def create_superuser(self, username, email, password):
        user = self.model(username=username, email=self.normalize_email(email))
        user.set_password(password)
        user.is_superuser = True
        user.is_staff = True
        user.save(using=self.db)
        return user

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=256,unique=True)
    email = models.EmailField(max_length=256)
    organisation = models.ForeignKey(Organisation,on_delete=models.CASCADE,null=True)

    is_staff = models.BooleanField(default=False)
    
    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ['email']
    objects = MyUserManager()

    def __str__(self):
        return self.username

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

forms.py

class MyUserForm(forms.ModelForm):
    class Meta():
        model = models.MyUser
        fields = ('username','email','password','organisation')

class MyUserLoginForm(forms.ModelForm):
    password=forms.CharField(label="password",widget=forms.PasswordInput)

    class Meta:
        model=models.MyUser
        fields=("username","email","password","organisation")

views.py

class indexPage(TemplateView):
    template_name = 'accounts/inner_index.html'

class Create_Organisation(CreateView):
    model = models.Organisation
    fields = ('__all__')
    template_name  = 'accounts/organisation_form.html'
    success_url = reverse_lazy("accounts:inner_index")

class Create_Accounts(CreateView,LoginRequiredMixin):
    model = models.MyUser
    form_class = forms.MyUserForm
    template_name = 'accounts/create_account.html'
    success_url = reverse_lazy("accounts:inner_index")



class LoginPageView(View):
    template_name = 'accounts/login_account.html'
    form_class = forms.MyUserLoginForm

    def get(self, request):
        form = self.form_class()
        message = ''
        return render(request, self.template_name, context={'form': form, 'message': message})

    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            user = authenticate(
                username=form.cleaned_data['username'],
                email = form.cleaned_data['email'],
                password=form.cleaned_data['password'],
                organisation = form.cleaned_data['organisation'],
            )
            if user:
                login(request, user)
                return redirect('/accounts/')
        message = 'Login failed!'
        return render(request, self.template_name, context={'form': form, 'message': message})

urls.py

app_name = "accounts"

urlpatterns = [
    path('',views.indexPage.as_view(),name  = 'inner_index'),
    path('create_organisation/',views.Create_Organisation.as_view(),name='create_organisation'),
    path('create_account/',views.Create_Accounts.as_view(),name='create_account'),
    path('login_account/',views.LoginPageView.as_view(),name = 'login'),
    path('login_test/',views.Login_Test.as_view(),name='login_test'),
]

登录账号.html

{% extends 'base.html' %}
{% load bootstrap3 %}
{% block body_block %}
<form method="POST">
  <h1>LOGIN</h1>
  {% csrf_token %}
  {% bootstrap_form form %}
  <input type="submit" name="" value="Login">
</form>

{% endblock %}

settings.py

AUTH_USER_MODEL = 'accounts.MyUser'
LOGIN_REDIRECT_URL = '/accounts/login_account'
qvk1mo1f

qvk1mo1f1#

代码中有一个主要问题。也就是说,通过使用一个通用的CreateView来创建你的用户,它不会使用你的管理器,因此不会散列密码,所以你需要覆盖它:
views.py

class Create_Accounts(CreateView,LoginRequiredMixin):
    model = models.MyUser
    form_class = forms.MyUserForm
    template_name = 'accounts/create_account.html'
    success_url = reverse_lazy("accounts:inner_index")

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            self.model.objects.create_user(**form.cleaned_data)
        return redirect('accounts:login')

但是,您的主要问题是因为您试图使用登录数据验证表单,这就是为什么您会得到username already exists。因为ModelForm将尝试验证username,而username应该是`unique:
models.py (片段)

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=256,unique=True)
    ...

此外,默认身份验证backend不需要organisationemail(该表单仍然可以轻松呈现组件):
views.py

class LoginPageView(View):
    ...

    def get(self, request):
        ...

    def post(self, request):
        credentials = {
        'username': request.POST.get('username'),
        'password': request.POST.get('password')
        }
        user = authenticate(**credentials)

        if user:
            login(request, user)
            return redirect('/accounts/')

        message = 'Login failed!'
        return render(request, self.template_name, context={'message': message})

相关问题