用pycharm建立一個新專案
1.在pycharm中新建一個名為xuehan的專案,在xuehan 下有template、static檔案包,還有一個xuehan.py 檔案。在pycharm輸入python xuehan.py runserver 在瀏覽器輸入地址:127.0.0.2:5000/會出現hello world.
2.如何建立自己的專案
a) 新建一個mainapp的python package包,在其目錄下有一個__init__.py,將template、static移到其目錄下。並建立一個views.py 檔案
b) 在__init__建立一個app,具體方法為:
from flask import Flask
from mainapp.views import app
app = Flask(__name__)
app.env = 'development'
app.register_blueprint(blue)
c) 在views下建立檢視函式
from flask import Blueprint, render_template
blue = Blueprint('main', __name__)
@blue.route('/')
def index():
return render_template('index.html') # 在template下建立index.html
d) 在主目錄下建立一個manage.py 檔案
from flask_script import Manager
manager = Manager(app)
manager.run()
最後在pycharm下執行python manage.py runserver,這樣一個基本的flask專案就建好了