让你的flask网站先跑起来(一) https://blog.csdn.net/YouYuDeYan/article/details/121402152 https://blog.csdn.net/YouYuDeYan/article/details/121402152 ;中环境已经搭好了,那么,到了激动人心的时刻了.
最后的目录如下:

gitee代码可自行下载,记得切换分支到20211028update,再下点clone下载哦

1.先看到终点,那么我们回到起点开始冲啦.
写代码的时候到了!创建 flaskr
文件夹并且文件夹内添加 __init__.py
文件。 __init__.py
有两个作用:一是包含应用工厂;二 是告诉 Python flaskr
文件夹应当视作为一个包。
$ mkdir flaskr
flaskr/__init__.py
代码如下:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
create_app
是一个应用工厂函数,后面的教程中会用到。这个看似简单的函 数其实已经做了许多事情。
app = Flask(__name__, instance_relative_config=True)
创建 Flask 实例。__name__
是当前 Python 模块的名称。应用需要知道在哪里设置路 径,使用__name__
是一个方便的方法。instance_relative_config=True
告诉应用配置文件是相对于 instance folder 的相对路径。实例文件 夹在flaskr
包的外面,用于存放本地数据(例如配置密钥和数据 库),不应当提交到版本控制系统。- app.config.from_mapping() 设置一个应 用的缺省配置:
- SECRET_KEY 是被 Flask 和扩展用于保证数据安全的。在开发 过程中,为了方便可以设置为
'dev'
,但是在发布的时候应当使用 一个随机值来重载它。 DATABASE
SQLite 数据库文件存放在路径。它位于 Flask 用于存放 实例的 app.instance_path 之内。下 一节会更详细地学习数据库的东西。- app.config.from_pyfile() 使用
config.py
中的值来重载缺省配置,如果config.py
存在的话。 例如,当正式部署的时候,用于设置一个正式的SECRET_KEY
。 test_config
也会被传递给工厂,并且会替代实例配置。这样可以 实现测试和开发的配置分离,相互独立。- os.makedirs() 可以确保 app.instance_path 存在。 Flask 不会自 动创建实例文件夹,但是必须确保创建这个文件夹,因为 SQLite 数据库文 件会被保存在里面。
- @app.route() 创建一个简单的路由,这样在继续教 程下面的内容前你可以先看看应用如何运行的。它创建了 URL
/hello
和一个函数之间的关联。这个函数会返回一个响应,即一个'Hello, World!'
字符串。
2.运行flask
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
大概会输出这样的东西

访问 http://127.0.0.1:5000/hello可以看到 Hello, World!
字样哦.
这就算是你的最简单的flask程序
3.一个好的工具就像一把枪,能让你从”拳击手”变成”枪手”,这里建议安装使用pycharm或者vs code

更多请持续关注 ,让你的flask网站先跑起来(三)
Original: https://blog.csdn.net/YouYuDeYan/article/details/121403599
Author: YouYuDeYan
Title: 让你的flask网站先跑起来(二)
相关阅读
Title: 阿里云部署Django项目
- yum install mysql-devel
- yum install python3-devel
- pip3 install mysqlclient
- pip3 install uwsgi
uwsgi配置:
[uwsgi]
chdir = /www/wwwroot/pms
module = pms.wsgi:application
socket = /www/wwwroot/pms/uwsgi.sock
workers = 5
pidfile = /www/wwwroot/pms/uwsgi.pid
http = 127.0.0.1:8000
static-map = /static = /www/wwwroot/pms/static
uid = root
gid = root
master = true
vacuum = true
enable-threads = true
thunder-lock = true
harakiri = 30
post-buffering = 4096
daemonize = /www/wwwroot/pms/uwsgi.log
使用 uwsgi 运行项目:
uwsgi --http :8000 --chdir /www/wwwroot/pms --home=/path/to/env --module pms.wsgi
–home 指定virtualenv 路径,如果没有可以去掉;
–module pms.wsgi 指定项目的WSGI文件位置;
uwsgi --ini /www/wwwroot/pms/uwsgi.ini
Nginx配置:
server {
listen 80;
server_name 123.56.96.138;
charset utf-8;
error_log /var/logs/nginx/error.log error;
location / {
include uwsgi_params;
uwsgi_pass unix:/www/wwwroot/pms/uwsgi.sock;
uwsgi_connect_timeout 30;
}
location /static/ {
alias /www/wwwroot/pms/static;
}
Nginx命令:
nginx ---启动nginx服务
nginx -t ---检查配置文件
nginx -s stop ---停止nginx服务
nginx -s quit ---退出nginx服务
nginx -s reload ---重新加载nginx服务
8、上传Django项目文件。
Original: https://blog.csdn.net/zx_953/article/details/123121842
Author: zx_953
Title: 阿里云部署Django项目
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/310350/
转载文章受原作者版权保护。转载请注明原作者出处!