1.在本機任意盤建立文件一文件夾,如:sec04css
2.在Pycharm上找到sec04建立子目標site01html
3.建立虛擬環境,Pycharm左上角file--->New Project頁面,點擊New environment using 在site01文件夾創建子目錄 myvenv,點擊 ‘ok'建立完成。python
4.建立static、templates文件件,run.py文件。jquery
templates用來放html文件。sql
run.py編程
from flask import Flask,request,render_template
import sqlite3
#鏈接數據源
conn = sqlite3.connect(r'.\db\books.db')
c = conn.cursor() #遊標
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/books/')
def book():
return render_template('books.html')
@app.route('/page/',methods=['GET'])
def page():
# categories = [(1, '網絡編程'), (2, '雲計算'), (3, '動物科學')]
conn = sqlite3.connect(r'.\db\books.db')
c = conn.cursor()
sql = 'select ROWID,Book_sortName from book_sort'
categories = c.execute(sql).fetchall()#執行sql語句
c.close()
conn.close()
return render_template('page.html',categories=categories)
@app.route('/page_box/',methods=['POST'])
def page_box():
if request.method == 'POST':
# 獲取表單
subject = request.form['subject']
if __name__ == '__main__':
app.run()
books.htmlflask
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>北京大學圖書館</title>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">北京大學線上圖書館PAM</a>
</div>
<div>
<!--左側 IOS、SVN、Java-->
<ul class="nav navbar-nav">
<li class="active"><a href="#">首頁</a></li>
<li><a href="#">圖書</a></li>
<li class="dropdown">
<a href="#" class="fropdown-toggle" data-toggle="dropdown">
圖書管理
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">Jmeter</a></li>
<li><a href="#">python</a></li>
<!--<li><a href="#">C</a></li>-->
<!--<li class="divider"></li>-->
<!--<li><a href="#">Php</a></li>-->
</ul>
</li>
</ul>
<!--右側退出欄-->
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="fropdown-toggle" data-toggle="dropdown">
Login
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">Jmeter</a></li>
<li class="divider"></li>
<li><a href="#">退出</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div class="row">
<ul class="breadcrumb">
<li><a href="/">首頁</a></li>
<li><a href="/">首頁</a></li>
</ul>
</div>
<div>
<div class="row">
{% block main_content %}
work
{% endblock %}
</div>
</div>
<!--內容底部-->
<div class="row">
<p class="breadcrumb ">版權</p>
</div>
</div>
</body>
</html>
在run.py右擊 選擇run執行文件,用路徑 http://127.0.0.1:5000/books/ 在瀏覽器訪問。