我正在做一个Flask应用程序,我需要做HTTP基本身份验证,但是它不起作用
我在做例子
为了解决此问题,我需要做什么?
导入的库
from flask import Flask, render_template, render_template_string, request
import json
带有身份验证的代码片段
@app.route('/user/<string:name>')
def user(name):
request.authorization.username
request.authorization.password
with open('json/users.json', 'rt', encoding='utf8') as f:
x = f.read()
data = json.loads(x)
user = data[name]
return render_template('about_user.html',
_name=user['name'],
_age=user['age'],
_discord=user['discord']
)
一个错误
AttributeError: 'NoneType' object has no attribute 'username'
您需要检查是否有request.authorization
第一个
if request.authorization:
request.authorization.username
request.authorization.password
with open('json/users.json', 'rt', encoding='utf8') as f:
x = f.read()
data = json.loads(x)
user = data[name]
return render_template('about_user.html',
_name=user['name'],
_age=user['age'],
_discord=user['discord']
)
else:
return make_response(....)
确保您导入make_response
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句