当前位置: 首页 > 知识库问答 >
问题:

TypeError:login()获得意外的关键字参数“template\u name”

马清野
2023-03-14

我将我的项目从Django 1.11升级到2.2,做了所有的更改,但带来了新的错误,说login()得到了一个意想不到的关键字参数template_name。它与Django 1.11的前一个版本运行良好(所有其他网址都在工作,只有着陆页给出了错误)。我找不到任何关于这个问题的参考资料。以下是该问题的错误、网址和视图。

Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\User\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\User\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\User\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\PycharmProjects\MyWebsite\landing\views.py", line 43, in landing_validation
    login_response = login(request, template_name='landing.html')
TypeError: login() got an unexpected keyword argument 'template_name'
[19/Apr/2019 16:20:00] "GET / HTTP/1.1" 500 71948

着陆\urls.py

from django.conf.urls import url
from landing.views import landing_validation

app_name='landing'
urlpatterns = [
    url(r'^$', landing_validation, name='landing')
]

着陆\views.py

from django.contrib.auth import login 

def landing_validation(request):
  login_response = login(request, template_name='landing.html')

  return login_response

C:\Users\User\venv\Lib\site packages\django\contrib\auth\uuuuu init\uuuuuu。派克

def login(request, user, backend=None):

    """
    Persist a user id and a backend in the request. This way a user doesn't
    have to reauthenticate on every request. Note that data set during
    the anonymous session is retained when the user logs in.
    """
    session_auth_hash = ''
    if user is None:
        user = request.user
    if hasattr(user, 'get_session_auth_hash'):
        session_auth_hash = user.get_session_auth_hash()

    if SESSION_KEY in request.session:
        if _get_user_session_key(request) != user.pk or (
                session_auth_hash and
                not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)):
            # To avoid reusing another user's session, create a new, empty
            # session if the existing session corresponds to a different
            # authenticated user.
            request.session.flush()
    else:
        request.session.cycle_key()

    try:
        backend = backend or user.backend
    except AttributeError:
        backends = _get_backends(return_tuples=True)
        if len(backends) == 1:
            _, backend = backends[0]
        else:
            raise ValueError(
                'You have multiple authentication backends configured and '
                'therefore must provide the `backend` argument or set the '
                '`backend` attribute on the user.'
            )
    else:
        if not isinstance(backend, str):
            raise TypeError('backend must be a dotted import path string (got %r).' % backend)

    request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
    request.session[BACKEND_SESSION_KEY] = backend
    request.session[HASH_SESSION_KEY] = session_auth_hash
    if hasattr(request, 'user'):
        request.user = user
    rotate_token(request)
    user_logged_in.send(sender=user.__class__, request=request, user=user)

共有3个答案

艾雪风
2023-03-14

您正在使用的login函数实际上不是一个视图。它只是一个常规函数,您可以使用它来验证用户身份并设置会话cookie等。

请阅读文档了解其用法:https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.login

根据您的代码判断,您似乎想要使用LoginView

from django.contrib.auth.views import LoginView

def landing_validation(request):
    login_response = LoginView.as_view()(request, template_name='landing.html')

    return login_response
孔飞翔
2023-03-14

在Django 2.2应用程序中,我通过使用LoginView而不是login修复了类似错误。

文件用户/URL。py:

from django.conf.urls import url
# from django.contrib.auth.views import login
from django.contrib.auth.views import LoginView

from . import views

app_name = 'users'
urlpatterns = [
    # url(r'^login/$', login, {'template_name': 'users/login.html'}, name='login'),
    url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
祁俊拔
2023-03-14

没有必要做landing_validation查看这条线解决了这个问题。

url(r'^$', LoginView.as_view(template_name='landing.html'), name='landing')
 类似资料:
  • 我尝试使用pandas DataFrame的pivot_table方法; 但是,我收到以下错误: 上述命令摘自Wes McKinney(pandas的创建者)的《Python用于数据分析》一书

  • 我有wiev功能: 装饰: "index"函数正常工作,但"细节"向下错误: TypeError:包装器()获得意外的关键字参数“id” P.S.id参数在url模式中

  • 我试图用Python的Pymoo库设置我的优化,我使用他们的“入门”指南,但传递我自己的独立变量,也不使用约束。我使用指南中的示例函数得到了相同的结果(我在下面的代码中注释了它们)。 代码如下: 当我打印出问题类中_evaluate_elementwise方法中的kwargs时,我确实得到了它是算法对象: {'算法': 我很难理解它是如何将algorithm对象作为_evalute的参数的,它接受

  • 我试图使用以下代码从上面的数据框创建一个有序的类别- 但是它给出了错误:astype()得到了一个意外的关键字参数“categories”。

  • 我正在尝试转换大熊猫的unix时间。我从一个csv文件中读取了这个,但是当我试图转换它时,我得到了上面的错误。 完全回溯 附加信息: 熊猫的版本是:0.8。0 操作系统:Debian,使用sudo apt get install python pandas安装(根据官方网站) 样本数据

  • 我有一张大桌子,我根据它们的日期把它切成许多小桌子: 我已经对dfs['2019-06-23']特定表进行了一些修改,现在我想将其保存在我的计算机上。我尝试了两种方法: 他们都提出了这个错误: get_handle()得到了一个意外的关键字参数错误 我不知道为什么会出现这个错误,也没有找到任何原因。我用这种方式保存了很多文件,但以前从未用过。 我的目标是:在修改后将此数据帧保存为csv