目前我有以下代码来处理传入的 GET 请求:
#view.py
def handle_request(request):
if request.method == 'GET':
<do something>
return response
此代码可以处理表单的简单 GET 请求:
curl http://some_url/
但现在我想添加基本的 http 身份验证:
curl --user username:password http://some_url/
我想修改我的 views.py 代码看起来像:
def handle_request(request):
if request.method == 'GET':
if username == some_hard_coded_approved_username and password == corresponding_password:
<do something>
return response
else:
response = HttpResponse("")
response.status_code = 401
return response
如何实现这一行来解析来自 http 请求的用户名和密码:
if username == some_hard_coded_approved_username and password == corresponding_password: