Ant*_*hez 5 python authentication django-2.1
I\xc2\xb4m 尝试使用 Django 登录没有密码的用户,到目前为止我已经创建了这个:
\n无密码.py
\nclass PasswordlessAuthBackend(ModelBackend):\n \n def authenticate(self, username=None):\n try:\n return User.objects.get(username=username)\n except User.DoesNotExist:\n return None\n\n def get_user(self, user_id):\n try:\n return User.objects.get(pk=user_id)\n except User.DoesNotExist:\n return None\nRun Code Online (Sandbox Code Playgroud)\n视图.py
\nfrom .Nopassword import PasswordlessAuthBackend\nuser = User()\np = PasswordlessAuthBackend()\nuser.username = p.get_user(user_id=None)\nuser = p.authenticate(username=user.username)\n\n\ndef home(request):\n login(request, user)\nRun Code Online (Sandbox Code Playgroud)\n问题是,当我运行 web 应用程序时,我\xc2\xb4m 收到此错误:
\nTraceback (most recent call last):\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner\n response = get_response(request)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response\n response = self.process_exception_by_middleware(e, request)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response\n response = wrapped_callback(request, *callback_args, **callback_kwargs)\n File "/home/bohosul02/misitio/gfgauth/views.py", line 29, in home\n login(request, user)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/views.py", line 125, in login\n )(request)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view\n return self.dispatch(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper\n return bound_func(*args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper\n return view(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func\n return func.__get__(self, type(self))(*args2, **kwargs2)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper\n return bound_func(*args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view\n response = view_func(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func\n return func.__get__(self, type(self))(*args2, **kwargs2)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper\n return bound_func(*args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func\n response = view_func(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func\n return func.__get__(self, type(self))(*args2, **kwargs2)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/views.py", line 66, in dispatch\n return super().dispatch(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch\n return handler(request, *args, **kwargs)\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/edit.py", line 133, in get\n return self.render_to_response(self.get_context_data())\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 126, in render_to_response\n template=self.get_template_names(),\n File "/home/bohosul02/.virtualenvs/myproject/lib/python3.6/site-packages/django/views/generic/base.py", line 139, in get_template_names\n "TemplateResponseMixin requires either a definition of "\ndjango.core.exceptions.ImproperlyConfigured: TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'\nRun Code Online (Sandbox Code Playgroud)\n我搜索了有关此错误的信息,但我发现当所有人遇到此错误时,这是因为他们在views.py文件中创建了一个类。
\n我该如何解决这个问题?
\n姜戈版本:2.1
\n如果您想在没有密码的情况下登录用户,则必须使用基于类的视图。如果您想在函数(def home)中执行此操作,则\xc2\xb4s将不起作用,正确的方法是\xc2\xb4s创建一个类(class home)。
\n请记住,一旦创建了模板,class home您就必须设置一个 template_name。
from django.views.generic import View\n\nclass Home(View)\n template_name = 'path/to/file.html'\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
6758 次 |
| 最近记录: |