>本教程演示了使用輕量級Python Web框架構建一個簡單的兩頁網站。 它專注於靜態內容來建立基礎工作流,很容易擴展到更複雜的應用程序。
>
檢查是否已經安裝了Virtualenv:
以下圖說明了應用程序流:
用戶請求(例如,
> app/app/templates/home.html
>中的模板:>
> :( main.css的內容保持不變)>
運行該應用程序並訪問 添加關於頁面和導航 創建“關於”模板:
update
app/utaes.py
>
>將導航樣式添加到> :(內容保持不變) >的大約頁面 >本教程演示了一個基本的燒瓶應用程序,說明了用於構建更複雜的Web應用程序的可擴展工作流程。 燒瓶的簡單性和功率使其成為各種網絡開發項目的絕佳選擇。>我們將使用Virtualenv為該項目創建一個孤立的Python環境。 這樣可以防止與其他系統庫發生衝突。
<code class="language-bash">$ virtualenv --version</code>
<code class="language-bash">$ pip install virtualenv</code>
<code class="language-bash">$ virtualenv flaskapp
$ cd flaskapp
$ . bin/activate</code>
<code class="language-bash">pip install Flask</code>
項目結構
在目錄中組織您的項目如下:
flaskapp
<code>flaskapp/
├── app/
│ ├── static/
│ │ ├── css/
│ │ ├── img/
│ │ └── js/
│ ├── templates/
│ ├── routes.py
│ └── README.md
└── ...</code>
)到達
。
首先,創建一個基本佈局模板:/
>在routes.py
文件夾中找到相應的模板。 routes.py
>文件夾中訪問靜態資產(圖像,CSS,JavaScript)。 templates
static
routes.py
為避免重複的HTML樣板,我們將使用Web模板。 燒瓶利用Jinja2模板引擎。 <code class="language-html"><!DOCTYPE html>
<title>Flask App</title>
<link href="%7B%7B%20url_for('static',%20filename='css/main.css')%20%7D%7D" rel="stylesheet">
<div class="container">
<h1 class="logo">Flask App</h1>
</div>
<div class="container">
{% block content %}{% endblock %}
</div>
</code>
<code class="language-html">{% extends "layout.html" %}
{% block content %}
<div class="jumbo">
<h2>Welcome!</h2>
<h3>This is the home page.</h3>
</div>
{% endblock %}</code>
routes.py
<code class="language-python">from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
if __name__ == '__main__':
app.run(debug=True)</code>
>
static/css/main.css
http://localhost:5000/
>
app/app/templates/about.html
<code class="language-html">{% extends "layout.html" %}
{% block content %}
<h2>About</h2>
<p>This is the About page.</p>
{% endblock %}</code>
routes.py
添加導航鏈接到<code class="language-python">from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)</code>
現在,您可以訪問關於結論
以上是Python燒瓶框架的簡介的詳細內容。更多資訊請關注PHP中文網其他相關文章!