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 鄭州做人流多少錢
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服務,解決方法如下:
啟動後就沒問題了
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2678247/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在 Visual Studio Code 中配置 Python Flask 環境PythonFlask
- Ubuntu 22.04 + Pycharm + Flask 配置 Flask 專案UbuntuPyCharmFlask
- Python Flask Web教程001:Flask簡介PythonFlaskWeb
- Flask——配置格式及使用Flask
- python 中的 flaskPythonFlask
- python的flask框架PythonFlask框架
- Flask基礎環境配置Flask
- 【python Flask】用uwsgi 啟動flask 服務方式PythonFlask
- python-flask學習PythonFlask
- python flask 設定cookiePythonFlaskCookie
- Python之Flask框架一PythonFlask框架
- python flask 定時器PythonFlask定時器
- python怎麼安裝flaskPythonFlask
- [python][flask] Jinja 模板入門PythonFlask
- nginx+uwsgi+flask 伺服器配置NginxFlask伺服器
- 原始碼解析Flask的配置檔案原始碼Flask
- springboot java呼叫flask python寫的Spring BootJavaFlaskPython
- 【Python】【001】建立第一個 FlaskPythonFlask
- Python如何匯入flask_sqlalchemyPythonFlaskSQL
- python flask pyecharts kline 01.pyPythonFlaskEcharts
- Python flask-restful框架講解PythonFlaskREST框架
- [python][flask] Flask 入門(以一個部落格後臺為例)PythonFlask
- 從原始碼看Flask框架配置管理原始碼Flask框架
- Flask快速入門day 01(flask介紹、快速使用、配置檔案、路由系統)Flask路由
- python 新版本flask建立介面方式PythonFlask
- 如何使用 Flask 編寫 Python Web APIFlaskPythonWebAPI
- python Flask框架學習——Response響應PythonFlask框架
- 大白話說Python+Flask入門(六)Flask SQLAlchemy操作mysql資料庫PythonFlaskMySql資料庫
- Python3 flask nginx uwsgi 環境搭建PythonFlaskNginx
- Python全棧Web(Flask框架、多表關聯)Python全棧WebFlask框架
- Python虛擬環境 flask快速啟動PythonFlask
- python web開發-flask中日誌的使用PythonWebFlask
- 快速上手python的簡單web框架flaskPythonWeb框架Flask
- flask實現python方法轉換服務FlaskPython
- https如何使用python+flask來實現HTTPPythonFlask
- Python基礎知識之常用框架Flask!Python框架Flask
- Python教程系列之Flask框架的學習PythonFlask框架
- ImportError: No module named flask 導包失敗,Python3 重新安裝 Flask 模組ImportErrorFlaskPython