简体   繁体   中英

flask_SQLAlchemy and Blueprints

I am new to Python I have been following some tutorials and now I am trying to make a site with an API on my own. I am using Flask, Flask_SQLAlchemy and Blueprints and I was trying to create an SQLite3 Database only to be accessible by the API. (I have other blueprints called site and admin=

This is my file structure (only for the API part, I don't think the site and admin structure is relevant let me know if I am wrong):

hspmng/
+-- run.py
+-- app/
    +-- __init__.py
    +-- api/
        +-- __init___.py
        +-- routes.py

Code:

/hspmng/run.py

from app import app
app.run (debug=True)

/hspmng/app/__init__.py

from flask import Flask
from app.api.routes import mod
from app.site.routes import mod
from app.admin.routes import mod

app = Flask(__name__)
app.register_blueprint(api.routes.mod, url_prefix='/api')
app.register_blueprint(site.routes.mod)
app.register_blueprint(admin.routes.mod, url_prefix='/admin')

/hspmng/app/api/__init__.py

file is empty

/hspmng/app/api/routes.py

My idea is to have a different file for db creation but I put it into routes just for the sake of testing it.

from flask import Blueprint
from flask_sqlalchemy import SQLAlchemy

mod = Blueprint('api', __name__)

mod.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
mod.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqllite3'

db = SQLAlchemy(mod)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    location = db.Column(db.String(50))


@mod.route('/getStuff')
def getStuff():
    return '<p>API</p>'

Database creation

To create the Database I am running python in my command line and typing the following

from app import db

and I am getting the following error

>>> from app import db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/d/Projects/hspmng/app/__init__.py", line 2, in <module>
    from app.api.routes import mod
  File "/mnt/d/Projects/hspmng/app/api/routes.py", line 5, in <module>
    mod.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
AttributeError: 'Blueprint' object has no attribute 'config'

Can you help me?

Move the following two lines where you set your config, to "app/____init___.py" where you create the app object.
config is not part of the Blueprint class, it is part of your Flask Application Object.

mod.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
mod.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqllite3'

In "app/__init__.py" you should be doing.

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqllite3'

This is so that your database URL lives on the application context so that all of your blueprints have access to it.

You can see more details about Flask.config here: http://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM