【问题标题】:Django API REST error using Postman: "detail": "Authentication credentials were not provided."使用 Postman 的 Django API REST 错误:“详细信息”:“未提供身份验证凭据。”
【发布时间】:2020-12-21 18:27:44
【问题描述】:

编辑

Operations to perform:
  Apply all migrations: admin, auth, authtoken, contenttypes, sessions
Running migrations:
  Applying authtoken.0001_initial... OK
  Applying authtoken.0002_auto_20160226_1747... OK
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv       
    connections.close_all()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\utils.py", line 224, in close_all
    connection.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db 
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable

我了解了 Django API REST 并实施了快速入门项目 (https://www.django-rest-framework.org/tutorial/quickstart/)。 它在本地运行良好,但我想将 API 与 Postman 一起使用,但它不起作用

我收到 401 未经授权的错误 我尝试在 settings.py 中添加“DEFAULT_AUTHENTICATION_CLASSES”键,但dose'nt 都不起作用

settings.py

INSTALLED_APPS = [
    'rest_framework',
    'rest_framework.authtoken',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10
}

此外,即使 py manage.py runserver 工作,我在保存项目时(如下)在终端中有大量回溯

Traceback (most recent call last):
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 196, in finish_response
    self.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\servers\basehttp.py", line 111, in close
    super().close()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\simple_server.py", line 38, in close
    SimpleHandler.close(self)
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 334, in close
    self.result.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\http\response.py", line 252, in close
    signals.request_finished.send(sender=self._handler_class)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\dispatch\dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\dispatch\dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\__init__.py", line 57, in close_old_connections
    conn.close_if_unusable_or_obsolete()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\base\base.py", line 514, in close_if_unusable_or_obsolete
    self.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db 
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable
[02/Sep/2020 16:34:13] "GET /users/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 57191)
Traceback (most recent call last):
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 196, in finish_response
    self.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\servers\basehttp.py", line 111, in close
    super().close()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\simple_server.py", line 38, in close
    SimpleHandler.close(self)
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 334, in close
    self.result.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\http\response.py", line 252, in close
    signals.request_finished.send(sender=self._handler_class)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\dispatch\dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\dispatch\dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\__init__.py", line 57, in close_old_connections
    conn.close_if_unusable_or_obsolete()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\base\base.py", line 514, in close_if_unusable_or_obsolete
    self.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db 
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
    self.handle_one_request()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\servers\basehttp.py", line 194, in handle_one_request 
    handler.run(self.server.get_app())
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 145, in run
    self.handle_error()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\servers\basehttp.py", line 116, in handle_error       
    super().handle_error()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 381, in handle_error
    self.finish_response()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 184, in finish_response
    self.write(data)
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 287, in write
    self.send_headers()
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 344, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Users\jl3.PRT-063\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 357, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
----------------------------------------
D:\Users\jl3\DevSpace\api_django\tutorial\settings.py changed, reloading.
Traceback (most recent call last):
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv       
    self.execute(*args, **cmd_options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 95, in handle 
    self.run(**options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 102, in run   
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 598, in run_with_reloader       
    start_django(reloader, main_func, *args, **kwargs)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 583, in start_django
    reloader.run(django_main_thread)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 301, in run
    self.run_loop()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 307, in run_loop
    next(ticker)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 355, in tick
    self.notify_file_changed(filepath)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 330, in notify_file_changed     
    trigger_reload(path)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 218, in trigger_reload
    sys.exit(3)
SystemExit: 3

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv       
    connections.close_all()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\utils.py", line 224, in close_all
    connection.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db 
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable
Traceback (most recent call last):
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 95, in handle 
    self.run(**options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\commands\runserver.py", line 102, in run   
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\utils\autoreload.py", line 601, in run_with_reloader       
    sys.exit(exit_code)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv       
    connections.close_all()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\utils.py", line 224, in close_all
    connection.close()
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db 
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\jl3.PRT-063\Desktop\Python\Django\env\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable

【问题讨论】:

    标签: django


    【解决方案1】:

    试试这个。

    python3 manage.py migrate
    

    【讨论】:

    【解决方案2】:

    首先,您必须为您尝试登录的用户创建一个令牌。

    from rest_framework.authtoken.models import Token
    
    token = Token.objects.create(user=...)
    print(token.key)
    

    然后你想在你的 url 头中包含那个令牌

    curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
    

    这是 TokenAuthentication 下的一些文档。 https://www.django-rest-framework.org/api-guide/authentication/

    在 post man 中,您只需单击标题框并在此处输入您的令牌。这应该对您进行身份验证,您只需首先为您的用户生成令牌。

    这里是一些关于如何使用 postman 处理常见身份验证类型的文档的链接。 https://learning.postman.com/docs/sending-requests/authorization/#basic-auth

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2014-10-07
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2017-11-22
      • 2015-05-16
      相关资源
      最近更新 更多