使用函数错误的Django登录:join()参数必须为str或字节,而不是'User'

时间:2019-08-22 10:07:46

标签: python django

我将Django 2.2.4用于登录功能

from django.contrib.auth import authenticate, login

def login_ajax(request):
    response = {'complete': False}

    if request.method == 'POST':
        data = json.loads(request.body)
        email, password = data['email'], data['password']

        user = authenticate(request, username=email, password=password)

        if user is None:
            #try with email
            user = authenticate(request, email=email, password=password)

        #both checks done so invalid
        if user is None:
            response['error'] = 'Username/Email and Password Combination Failed.'
        else:
            print(user.id)
            login(request, user)
            response['complete'] = True
    else:
        response['error'] = 'no post data found'

    return HttpResponse(
            json.dumps(response),
            content_type="application/json"
        )

可以使用基于Django's documentation的用户名/电子邮件登录,但会引发此错误

TypeError at /login-ajax
join() argument must be str or bytes, not 'User'

Request Method: POST
Request URL: http://127.0.0.1:8000/login-ajax
Django Version: 2.2.4
Python Executable: C:\Users\samuel.irungu\Envs\chuify\Scripts\python.exe
Python Version: 3.7.4
Python Path: ['C:\\Users\\samuel.irungu\\code\\chuify', 'C:\\Users\\samuel.irungu\\Envs\\chuify\\Scripts\\python37.zip', 'C:\\Users\\samuel.irungu\\Envs\\chuify\\DLLs', 'C:\\Users\\samuel.irungu\\Envs\\chuify\\lib', 'C:\\Users\\samuel.irungu\\Envs\\chuify\\Scripts', 'c:\\users\\samuel.irungu\\appdata\\local\\programs\\python\\python37\\Lib', 'c:\\users\\samuel.irungu\\appdata\\local\\programs\\python\\python37\\DLLs', 'C:\\Users\\samuel.irungu\\Envs\\chuify', 'C:\\Users\\samuel.irungu\\Envs\\chuify\\lib\\site-packages']
Server time: Thu, 22 Aug 2019 10:03:59 +0000
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'common']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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 "C:\Users\samuel.irungu\Envs\chuify\lib\ntpath.py" in join
89.         for p in map(os.fspath, paths):

During handling of the above exception (expected str, bytes or os.PathLike object, not User), another exception occurred:

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\core\handlers\exception.py" in inner
34.             response = get_response(request)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\core\handlers\base.py" in _get_response
115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\core\handlers\base.py" in _get_response
113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\samuel.irungu\code\chuify\chuify\views.py" in login_ajax
40.             login(request, user)

File "C:\Users\samuel.irungu\code\chuify\chuify\views.py" in login
18.     return render(request, template_name, context)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\shortcuts.py" in render
36.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\loader.py" in render_to_string
61.         template = get_template(template_name, using=using)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\loader.py" in get_template
15.             return engine.get_template(template_name)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\backends\django.py" in get_template
34.             return Template(self.engine.get_template(template_name), self)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\engine.py" in get_template
143.         template, origin = self.find_template(template_name)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\engine.py" in find_template
125.                 template = loader.get_template(name, skip=skip)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\loaders\base.py" in get_template
18.         for origin in self.get_template_sources(template_name):

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\template\loaders\filesystem.py" in get_template_sources
36.                 name = safe_join(template_dir, template_name)

File "C:\Users\samuel.irungu\Envs\chuify\lib\site-packages\django\utils\_os.py" in safe_join
32.     final_path = abspath(join(base, *paths))

File "C:\Users\samuel.irungu\Envs\chuify\lib\ntpath.py" in join
115.         genericpath._check_arg_types('join', path, *paths)

File "C:\Users\samuel.irungu\Envs\chuify\lib\genericpath.py" in _check_arg_types
149.                             (funcname, s.__class__.__name__)) from None

据我所知,最好的方法似乎是通过身份验证,然后尝试使用模板,这可能是引发错误的原因。

0 个答案:

没有答案