python网页制作案例
时间: 2023-09-06 10:09:22 浏览: 37
以下是一个简单的 Python 网页制作案例:
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
```
这个程序使用 Flask 框架创建了一个简单的网站,其中包含首页和关于页面。`render_template` 函数用于渲染 HTML 模板,这些模板分别存储在名为 `templates` 的文件夹中。在运行该程序时,可以通过浏览器访问 `http://localhost:5000/` 和 `http://localhost:5000/about` 来查看这两个页面。
以下是 `index.html` 和 `about.html` 的简单示例:
```html
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>Here you can find information about me and my interests.</p>
<a href="/about">Learn more about me</a>
</body>
</html>
```
```html
<!-- about.html -->
<!DOCTYPE html>
<html>
<head>
<title>About me</title>
</head>
<body>
<h1>About me</h1>
<p>My name is John Doe and I am a software developer based in San Francisco.</p>
<a href="/">Back to homepage</a>
</body>
</html>
```
这只是一个简单的示例,但它可以作为一个起点,用于构建更复杂的网站。