第一个falsk程序

版权声明:FatPuffer https://blog.csdn.net/qq_42517220/article/details/88684035

项目结构:

.
├── hello.py
├── static
└── templates

hello.py文件内容:

# coding:utf-8

from flask import Flask                                                     
                                                                             
# 创建flask应用                                                             
app = Flask(__name__)                                                       
   
# 访问路由地址                                                                         
@app.route('/')                                                             
def index():                                                                
	return 'hello world'                                                    
                                                                            
if __name__ == '__main__':                                                  
	# 启动flask程序                                                         
   	app.run() 

说明:

  • __name__ 代表当前模块名
  • flask以这个模块所在的目录为总目录
  • 默认这个目录中的static为静态目录,templates为模板目录

启动flask应用

python hello.py

在这里插入图片描述
浏览器访问

http://127.0.0.1:5000/

在这里插入图片描述
接下来我们在static静态文件目录下创建index.html文件,内如下

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <title>Title</title>
</head>
<body>
    <h1>hello python flask</h1>                                             
</body>
</html>

通过浏览器直接访问该静态文件

http://127.0.0.1:5000/static/index.html

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42517220/article/details/88684035