如何在 django url 中传递请求

How to pass a request in django urls

我有这个 url: (Django 2.0)

re_path('auth/(?P<code_>[a-zA-Z0-9]{12})/(?P<email_>[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', homePageView.autenticate, name='autenticate'),

在我的 views.py:

class homePageView(TemplateView):
    template_name = 'index.html'

def autenticate(self, email_, code_):
    try:
        user = Usuario.objects.get(email=email_,activated_code=code_)
        code = user.two_factors_auth_code
        if (not user.two_factors_auth) and (code == code_) and (len(code) == 12):
            user.next_login = True
        user.save()
        return redirect('home')
    except Usuario.DoesNotExist:
        return redirect('home')

我希望我可以在我的身份验证方法中登录用户,但我的函数中没有请求参数。

¿如何将我的身份验证功能转换为另一个功能?

class homePageView(TemplateView):
    template_name = 'index.html'

def autenticate(self, request, email_, code_):
    try:
        user = Usuario.objects.get(email=email_,activated_code=code_)
        code = user.two_factors_auth_code
        if (not user.two_factors_auth) and (code == code_) and (len(code) == 12):
            user.next_login = True
        user.save()
        ####################
        login(request,user)
        ####################
        return redirect('home')
    except Usuario.DoesNotExist:
        return redirect('home')

您在基于函数的视图等视图中使用身份验证方法。

在此方法中,第一个参数是请求。仅将第一个参数重命名为请求及其完成