Viraj Kaulkar Viraj Kaulkar - 3 years ago 261
Python Question

Adding Flask-Restful resource raises 'str' object has no attribute '__name__'

I am trying to add route to flask app in python 3. But I am getting error at following line saying AttributeError: 'str' object has no attribute '__name__'

api.add_resource('/todo/api/v1.0/tasks', methods=['GET'])

My code snippet is as follows

from flask import Flask, jsonify,abort,make_response,request,url_for
from flask_restful import Api
from referme.common import db
app = Flask(__name__)
from referme.resources.tasks import Tasks
api = Api(app)
api.add_resource('/refer-me/api/v1.0/tasks', methods=['GET'])

Answer Source

You aren't passing the Tasks Resource to add_resource.

The docs say

api.add_resource(HelloWorld, '/')

so I think your invocation should be

api.add_resource(Tasks, '/refer-me/api/v1.0/tasks', methods=['GET'])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download