成都创新互联网站制作重庆分公司

python的flask怎么配置

这篇文章主要讲解了“python的flask怎么配置”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python的flask怎么配置”吧!

成都创新互联公司是一家专业提供屯昌企业网站建设,专注与网站建设、成都网站设计、HTML5、小程序制作等业务。10年已为屯昌众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

配置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 %}

以上是我写的所有代码,在配置过程中遇到了一些问题所以拿出来分享以下,希望能帮助到你们。

第一个问题

python的flask怎么配置

首先我发现上面的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怎么配置这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前标题:python的flask怎么配置
文章起源:http://cxhlcq.cn/article/goehgc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部