TemplateResponseMixin 需要 'template_name' 的定义或 'get_template_names() 的实现 - Django 2.1

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names() - Django 2.1

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

Nopassword.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

views.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 文件中创建了 class。

我该如何解决?

Django 版本:2.1

注意:我刚刚回答了你关于启用无密码登录的其他问题,虽然我提出了其他建议 - 你在这里的内容也应该有效。

错误非常简单

django.core.exceptions.ImproperlyConfigured: TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

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

中应该有这样的东西
class PasswordlessAuthBackend(ModelBackend):
    template_name = 'path/to/filename.html'

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

请注意,一旦您创建了 class home,您 必须 设置一个 template_name。

from django.views.generic import View

class Home(View)
   template_name = 'path/to/file.html'