英文:
Flask SQLAlchemy created an instance folder?
问题
Flask应用程序。
我有一个使用SQLite的数据库,名为market.db
。我已在我的app.py
中进行了配置,如下所示:app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
然后当我运行flask shell
时,它创建了一个文件夹instance/
。然后我运行了db.create_all()
,它在instance/
文件夹中创建了一个数据库。我可以在同一个shell中向数据库添加项目,没有任何问题,但我担心这是否适用于生产环境。
文件层次结构:
最终,我希望将其部署到云服务器,并希望用户能够登录和注册。我担心的是,当用户注册时,它是否会保存这些更改到整个网站,还是仅保存在该用户的本地计算机上。我希望当用户注册时,允许网站上的其他用户访问其他用户,因为我将拥有所有用户的表格。谢谢。
app.py:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1024), nullable=False, unique=True)
@app.route("/")
@app.route('/home')
def home_page():
return render_template('home.html')
@app.route('/market')
def market_page():
items = [
{'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500},
{'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900},
{'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150}
]
return render_template('market.html', items=items)
英文:
Flask app.
I have a database using sqlite called market.db
. I have configured it in my app.py
like this: app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
Then when I run flask shell
, it created a folder instance/
. Then I ran db.create_all()
, and that created a database inside of that instance/
folder. I can add items to the database no problems while in the same shell, but I am worried about this being production worthy.
File Hierarchy:
I want to deploy this eventually to a cloud server, and I want people to be able to login and register. What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person's local machine. I want a way for when a user registers, to allow other people on the website to access those other users, because I will have a table of all of the users. Thank you.
app.py:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1024), nullable=False, unique=True)
@app.route("/")
@app.route('/home')
def home_page():
return render_template('home.html')
@app.route('/market')
def market_page():
items = [
{'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500},
{'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900},
{'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150}
]
return render_template('market.html', items=items)
答案1
得分: 0
不,它不会保存在用户的本地机器上,记住,您将在云上托管您的Flask应用。整个Flask应用以及实例文件夹将位于云上,因此它将保存到云上的DB文件实例中。实例文件夹的目的是保存与您的应用程序实例相关的敏感信息,通常您不希望将其提交到远程存储库。这也不会自动发生,只有在将实例文件夹添加到.gitignore
文件中时才会发生。
英文:
> What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person's local machine.
>
No, it won’t save on the user’s local machine, remember you will be hosting your flask app on the cloud. the whole flask app along with the instance folder will be on the cloud, so it will save to the instance of the DB file on the cloud. The purpose of the instance folder is to hold sensitive information that is specific to that instance of your application which normally you don’t want to commit to a remote repository. This too does not happen automatically, it only happens if you add the instance folder to the .gitignore
file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论