每当我尝试使用flask_httpauth为flask_restful项目启用身份验证时,我都会收到此错误:
AttributeError: 'function' object has no attribute 'as_view'
这是一个非常基本的例子: apicontroller.py:
from flask_restful import Resource, Api
from flasktest import api, app
from flask_httpauth import HTTPTokenAuth
auth = HTTPTokenAuth()
@auth.login_required
class ApiController(Resource):
def get(self):
return True
api.add_resource(ApiController, '/api/apicontroller')
初始化的.py:
from flask import render_template
from flask import Flask, request, render_template, session, flash, redirect, url_for, jsonify
from flask_restful import Resource, Api, reqparse, fields
from flask_httpauth import HTTPTokenAuth
app = Flask(__name__)
api = Api(app)
import flasktest.apicontroller
每当我用@auth.login_required
装饰控制器类时,它都会因上述错误而失败。我该如何解决这个问题?
答案 0 :(得分:3)
我相信你不能不将装饰器应用到课堂上。
要解决这个问题:
from flask_restful import Resource, Api
from flasktest import api, app
from flask_httpauth import HTTPTokenAuth
auth = HTTPTokenAuth()
class ApiController(Resource):
decorators = [auth.login_required]
def get(self):
return True
api.add_resource(ApiController, '/api/apicontroller')