编辑
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”键,但不起作用
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
答案 0 :(得分:0)
尝试这个。
python3 manage.py migrate
答案 1 :(得分:0)
首先,您将不得不为尝试登录的用户创建令牌。
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/
在邮递员中,您只需单击标题框并在其中输入令牌即可。这样就可以对您进行身份验证,您只需要首先为用户生成令牌即可。
这里是一些文档的链接,这些文档介绍了如何与邮递员一起使用常见的身份验证类型。 https://learning.postman.com/docs/sending-requests/authorization/#basic-auth