>本教程演示了使用轻量级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中文网其他相关文章!