django:没有内置身份验证的用户登录和身份验证问题

时间:2020-04-15 23:42:50

标签: python django

我正在努力使用登录系统。在这一点上,我已经看到了关于该主题的许多不同内容,使我有些失落。我曾尝试使用内置的身份验证系统,但未能使其与django-tenant一起使用,因为如果我正确了解它,则需要与内置的User模型一起使用。

然后我想我的潜在问题是我不知道如何将Django用户模型与已有的模型集成。另一方面,我不知道该如何写一些东西来代替Django中内置的身份验证系统。

理想情况下,我想将自己的内容与内置于User中的Django集成在一起,以提供安全可靠的身份验证系统。

到目前为止,这是我独自想出的东西。它“运行”,但显然什么也没做!

Model.py

class Client(TenantMixin):

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, default='')
    email = models.EmailField(default='')
    company = models.CharField(max_length=100, default='')
    password = models.CharField(max_length=100, default='')
    created_on = models.DateField(auto_now_add=True)

class Domain(DomainMixin):
    pass

forms.py

class UserLoginForm(forms.Form):
    email = forms.CharField()
    password = forms.CharField(widget = forms.PasswordInput)
    company = forms.CharField()

    def cleaned_data(self):
        email = self.cleaned_data.get('email')
        password = self.cleaned_data.get('password')
        company = self.cleaned_data.get('company')

        try:
            tenant = Client.objects.get(email=email, password=password, company=company)

        except Client.DoesNotExist:
            raise forms.ValidationError("User does not exist")

        if email and password:
            user = authenticate(username= email, password= password)
            if not user:
                raise forms.ValidationError('THIS USER DOES NOT EXIST')

            if not user.check_password(password):
                raise forms.ValidationError('incorrect password')

            if not user.is_active:
                raise forms.ValidationError('this user is not active')

        return super(UserLoginForm, self).clean()

views.py

def login_view(request):
    form = UserLoginForm(request.POST or None)
    if form.is_valid():
        company = form.cleaned_data.get('company')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')

        ##tenant = authenticate(request, username=email, password=password)

        with schema_context(tenant.schema_name):

            redirect = 'http://' + tenant.company + '.inventory4.com:8000/upload'
        return HttpResponseRedirect(redirect)

    context = {
        'form' : form,
    }
    return render(request, 'login.html', context)

0 个答案:

没有答案