【问题标题】:authenticate() got an unexpected keyword argument 'username'验证()有一个意外的关键字参数“用户名”
【发布时间】:2014-01-23 09:09:52
【问题描述】:

我在尝试验证我的用户时在 django 中遇到了一个奇怪的错误。

代码如下:

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.context_processors import csrf
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login

def authenticate(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            # Redirect to a success page.
        else:
            pass
            # Return a 'disabled account' error message
    else:
        pass
        # Return an 'invalid login' error message.

它一直给我这个错误:

Environment:


Request Method: POST
Request URL: http://localhost:8000/authenticate/

Django Version: 1.6.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'neededform')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jarvis/django/web/web/views.py" in authenticate
  26.     user = authenticate(username=str(username), password=password)

Exception Type: TypeError at /authenticate/
Exception Value: authenticate() got an unexpected keyword argument 'username'

我无法理解这个错误的来源。有人遇到同样的问题吗?

【问题讨论】:

  • 您也调用了函数身份验证(需要请求)?更改名称,使其不与内置冲突

标签: python django authentication django-authentication


【解决方案1】:

代码使用authenticate 作为视图名称。这会覆盖对导入函数 authenticate 的引用。

from django.contrib.auth import authenticate, login
#                               ^^^^^^^^^^^^

def authenticate(request):
#   ^^^^^^^^^^^^

重命名视图:

def other_view_name(request):

或将函数导入为不同的名称(也更改对函数的调用):

from django.contrib.auth import authenticate as auth
...

user = authenticate(...) -> user = auth(...)

或导入django.contrib.auth 并使用完全限定名称。

import django.contrib.auth
...

user = authenticate(...) -> user = django.contrib.auth.authenticate(...)

【讨论】:

  • import django.contrib.auth 并使用完全限定名称。不过,这会变得很尴尬。
  • @KarlKnechtel,感谢您的评论。我也加了。
  • @falsetru 原谅我的胡思乱想,但我很好奇,不会将authenticate 导入为auth 开始搞乱原来的auth ???
  • @Stark,是的,如果 auth 已经在当前命名空间(模块)中定义/导入,它可能会与 auth 混淆。
  • 救了我的命。
猜你喜欢
  • 2021-11-27
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 2020-07-27
相关资源
最近更新 更多