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

问题描述

我正在尝试使用 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

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,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 142,in _wrapped_view
    response = view_func(request,**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,**kwargs2)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/views.py",line 66,in dispatch
    return super().dispatch(request,**kwargs)
  File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py",line 89,in dispatch
    return handler(request,**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",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

解决方法

注意:我刚刚回答了您关于启用无密码登录的另一个问题,尽管我提出了其他建议 - 您在此处提供的内容也应该有效。

错误很简单

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

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

class PasswordlessAuthBackend(ModelBackend):
    template_name = 'path/to/filename.html'
,

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

请注意,一旦您创建了 class home,您必须设置一个模板名称。

from django.views.generic import View

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