Search code examples
pythonauthenticationdjango-2.1

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


I´m trying to log in a user without password using Django, until now I have created this:

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)

The problem is that when I run the webapp, I´m getting this error:

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()'

I have searched about information of this error, but I have found that when all people were experiencing this error, it was because they had created a class inside their views.py file.

How can I solve that?

Django version: 2.1


Solution

  • If you want to log in a user without password, you must use a class based views. It´s not going to work if you want to do it in a function (def home), the correct way to do it´s to create a class (class home).

    Remind that once you have created your class home you must set a template_name.

    from django.views.generic import View
    
    class Home(View)
       template_name = 'path/to/file.html'