2

我正在尝试使用 Django 登录没有密码的用户,直到现在我已经创建了这个:

无密码.py

class PasswordlessAuthBackend(ModelBackend):
   
    def authenticate(self, username=None):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

视图.py

from .Nopassword import PasswordlessAuthBackend
user = User()
p = PasswordlessAuthBackend()
user.username = p.get_user(user_id=None)
user = p.authenticate(username=user.username)


def home(request):
    login(request, user)

问题是,当我运行 webapp 时,我收到了这个错误:

Traceback (most recent call last):
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/bohosul02/misitio/gfgauth/views.py", line 29, in home
    login(request, user)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/views.py", line 125, in login
    )(request)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/views.py", line 66, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/edit.py", line 133, in get
    return self.render_to_response(self.get_context_data())
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 126, in render_to_response
    template=self.get_template_names(),
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 139, in get_template_names
    "TemplateResponseMixin requires either a definition of "
django.core.exceptions.ImproperlyConfigured: TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

我搜索了这个错误的信息,但是我发现当所有人都遇到这个错误时,那是因为他们在他们的views.py文件中创建了一个类。

我该如何解决?

Django 版本:2.1

4

2 回答 2

1

注意:我刚刚回答了您关于启用无密码登录的其他问题,尽管我提出了其他建议 - 您在这里所拥有的也应该可以工作。

错误非常简单

django.core.exceptions.ImproperlyConfigured:TemplateResponseMixin 需要“template_name”的定义或“get_template_names()”的实现

您的类定义需要template_name设置一个变量或调用模板文件路径的方法的实现。这是因为该类固有地使用一个TemplateResponseMixin 实现并且它需要一个模板文件。你的课上应该有这样的东西

class PasswordlessAuthBackend(ModelBackend):
    template_name = 'path/to/filename.html'
于 2020-12-31T08:37:59.350 回答
1

如果您想在没有密码的情况下登录用户,则必须使用基于类的视图。如果你想在一个函数(def home)中做它是行不通的,正确的方法是创建一个类(class home)。

提醒一下,一旦你创建了你的class home,你必须设置一个 template_name。

from django.views.generic import View

class Home(View)
   template_name = 'path/to/file.html'
于 2020-12-31T13:28:26.027 回答