Calling method from Flask Blueprint

Adam :

I am trying to call a get method from my main.py from another python file and I am using flask and blueprints.

I have 2 files: main.py and product.py.

Based on the documentation, I thought after we have done an import, we can then call the method.

In my product.py

import os
from flask import Blueprint, Flask, render_template, request, jsonify
import stripe
import json
import ast

product = Blueprint('product',__name__)


@product.route('/getallproducts', methods=['GET'])
def get_products ():
    myList = ["price_1GqLlRFswqvdSoNHi27H2wLV","price_1GqLiAFswqvdSoNHjv4R6scY","price_1GqY8eFswqvdSoNHVSRzEQdn","price_1GqYAcFswqvdSoNH1uReU4kN"]
    result =[]
    for i in myList:
        priceobj = stripe.Price.retrieve(i)
        product= stripe.Product.retrieve(priceobj.product)
        data = product
        data["price"] = priceobj.unit_amount/100
        data["priceid"] =i
        result.append(data)
    return result

In my main.py i have

import os
from flask import Blueprint, Flask, render_template, request, jsonify
import stripe
import json
import ast

from product import product


stripe.api_key = stripe_keys['secret_key']

app=Flask(__name__,template_folder='../client', static_url_path='', static_folder='../client')

app.register_blueprint(product)


@app.route('/', methods=['GET'])
def main():
    
    result =product.get_products()
    return render_template('index.html', data=result)

I tried to do product.get_products() but it complained that no such methods exist.

Is there something which I am missing out on as I thought this was the way we can use blueprints?

Leandro Esteban :

You're likely getting the invalid method because you're not importing the function get_products() but a variable from your file that uses it. Try changing the import line in your main.py to from product import product, get_products.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related