您好,登錄后才能下訂單哦!
這篇文章主要講解了“python的flask怎么配置”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“python的flask怎么配置”吧!
配置flask的一些問題
首先給上相關配置代碼
manage.py
import os
from flask_migrate import MigrateCommand
from App import creat_app
from flask_script import Manager
# env=os.environ.get('default')
app =creat_app(env='default')
manager=Manager(app)
manager.add_command("db",MigrateCommand)
if __name__ == '__main__':
manager.run()
init.py
from flask import Flask
from App.ext import init_ext
from App.settings import envs
from App.views import init_blue
def creat_app(env):
app=Flask(__name__)
app.config.from_object(envs.get(env))
init_ext(app)
init_blue(app)
return app
ext.py
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_bootstrap import Bootstrap
from flask_session import Session
db=SQLAlchemy()
migrate=Migrate()
def init_ext(app):
db.app=app
db.init_app(app)
migrate.init_app(app,db)
Session(app)
Bootstrap(app)
setting.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def get_db_uri(dbinfo):
engine = dbinfo.get("ENGINE")
driver = dbinfo.get("DRIVER")
user = dbinfo.get("USER")
password = dbinfo.get("PASSWORD")
host = dbinfo.get("HOST")
port = dbinfo.get("PORT")
name = dbinfo.get("NAME")
return '{}+{}://{}:{}@{}:{}/{}'.format(engine, driver, user, password, host, port, name)
class Config:
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY='Rock'
SESSION_TYPE='redis'
SESSION_COOKIE_SECURE=True
SESSION_USE_SIGNER=True
@staticmethod
def init_app(app):
pass
class DevelopConfig(Config):
DEBUG = True
dbinfo = {
"ENGINE": "mysql",
"DRIVER": "pymysql",
"USER": "root",
"PASSWORD": "123456",
"HOST": "localhost",
"PORT": "3306",
"NAME": "flask",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class TestConfig(Config):
TESTING = True
dbinfo = {
"ENGINE": "mysql",
"DRIVER": "pymysql",
"USER": "root",
"PASSWORD": "123456",
"HOST": "localhost",
"PORT": "3306",
"NAME": "flask",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class StagingConfig(Config):
dbinfo = {
"ENGINE": "mysql",
"DRIVER": "pymysql",
"USER": "root",
"PASSWORD": "123456",
"HOST": "localhost",
"PORT": "3306",
"NAME": "flask",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class ProductConfig(Config):
dbinfo = {
"ENGINE": "mysql",
"DRIVER": "pymysql",
"USER": "root",
"PASSWORD": "123456",
"HOST": "localhost",
"PORT": "3306",
"NAME": "flask",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
envs = {
'develop': DevelopConfig,
'testing': TestConfig,
'stage': StagingConfig,
'product': ProductConfig,
'default': DevelopConfig,
}
views.py鄭州做人流多少錢 http://www.120zzzzyy.com/
from flask import Blueprint, render_template, request, Response, session
blue=Blueprint('blue',__name__)
def init_blue(app):
app.register_blueprint(blue)
@blue.route("/login",methods=["GET","POST"])
def login():
if request.method=='GET':
return render_template("login.html")
elif request.method=="POST":
username=request.form.get("username")
# return render_template("login.html",username=username)
# response=Response("%s" % username)
# # response.set_cookie('username',username)
# session['username']=username
# return response
return render_template("login.html")
login.html
{% extends 'bootstrap/base.html' %}
{% block navbar %}
Toggle navigation
Brand
Link (current)
Link
Dropdown
Action
Another action
Something else here
Separated link
One more separated link
Submit
Link
Dropdown
Action
Another action
Something else here
Separated link
{% endblock %}
{% block content %}
{% endblock %}
{% block header %}
{% endblock %}
{% block container %}
{% endblock %}
{% block footer %}
{% endblock %}
以上是我寫的所有代碼,在配置過程中遇到了一些問題所以拿出來分享以下,希望能幫助到你們。
第一個問題
首先我發現上面的env=os.environ.get(‘default’)這個方法,在我操作的過程中是沒法將default傳到給creat_app(),因此,他就獲得不了default相關配置信息,于是運行后他就會報錯,如以下報錯信息
UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set.
以下是init_app中提取出來的信息
if (
'SQLALCHEMY_DATABASE_URI' not in app.config and
'SQLALCHEMY_BINDS' not in app.config
):
warnings.warn(
'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
'Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".'
)
可以看出他是沒有獲取到SQLALCHEMY_DATABASE_URI和SQLALCHEMY_BINDS才報錯的,而這個是寫在setting中的,所以我直接將default賦給env,之后他不會報錯。,也沒有出現這個報錯信息。
然后第二個問題:
SESSION_TYPE='redis'
我在運行的時候出現了這個問題
redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. 由于目標計算機積極拒絕,無法連接 是由于沒有啟動redis服務,解決方法如下:
啟動后就沒問題了
感謝各位的閱讀,以上就是“python的flask怎么配置”的內容了,經過本文的學習后,相信大家對python的flask怎么配置這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。