5

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

Antonio Sanchez
  • 381
  • 1
  • 3
  • 11

3 Answers3

3

Note: I just answered your other question on enabling login without password, and though I suggested something else - what you have here should also work.

The error is pretty straightforward

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

Your class definition requires a template_name variable to be set or an implementation of the method to invoke the path of the template file. This is because the class inherently uses a TemplateResponseMixin implementation and it requires a template file. You should have something like this in your class

class PasswordlessAuthBackend(ModelBackend):
    template_name = 'path/to/filename.html'
AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • I have added full path of 'template_name' to my class 'PasswordlessAuthBackend', but still the same error – Antonio Sanchez Dec 31 '20 at 11:01
  • @AntonioSanchez can you try extending the class name with `TemplateResponseMixin`. It should look like this `class PasswordlessAuthBackend(ModelBackend, TemplateResponseMixin)`. Does my answer on the other question help? – AzyCrw4282 Dec 31 '20 at 11:51
2

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'
Bohosul
  • 107
  • 8
0

Even though if you type

**templat_name = 'index.html'**

instead of

**template_name = 'index.html'**

you will get the below message:

**TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'**
Suresh Dooly
  • 47
  • 2
  • 7