Zion
Zion

Reputation: 1610

how to do a url_for in flask restful using blueprints?

code:

blueprint:

from flask import Blueprint
from flask_restful import Api
################################

### Local Imports ###

################################


profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *

views:

class ShowProfPic(Resource):
    def get(self):
        return "hey"

api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")  

how do we do a url_for with flask_restful? because when I do.

url_for('showpic') it's a routing error and when I do url_for('api.ShowProfPic') it's also still a routing error

Upvotes: 7

Views: 2377

Answers (1)

Zion
Zion

Reputation: 1610

I've got the answer.

Apparently when working with blueprints

the way to access flask_restful's url_for is

url_for('blueprint_name.endpoint)

meaning an endpoint has to be specified on the resource

so using the example above:

profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *


class ShowProfPic(Resource):
    def get(self):
        return "hey"

api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic") 

to reference the ShowProfPic class and get it's endpoint it's url_for('blueprint_name.endpoint') so that's url_for(profile_api.showpic)

Upvotes: 6

Related Questions