用python做一个网站,最伤放有5个链接,点击一个链接显示一张网络图片,主页是一个图片在另一个图片上面
时间: 2025-08-19 22:03:15 浏览: 0
<think>我们将使用Python的Flask框架来创建一个简单的网站。网站主页将显示两张叠加的图片(即一个图片在另一个图片上面),然后底部有5个链接,每个链接点击后显示一张网络图片。
步骤:
1. 安装Flask(如果尚未安装):`pip install flask`
2. 创建项目结构:
- 项目文件夹
- templates (存放HTML模板)
- index.html (主页)
- image.html (显示单张图片的页面)
- static (存放静态文件,如CSS、本地图片等,但这里我们使用网络图片,所以可能不需要)
- app.py (主程序)
3. 编写app.py
4. 编写HTML模板
由于题目要求使用网络图片,我们将直接使用图片的URL。
主页:我们使用两张网络图片,通过CSS实现叠加效果。
五个链接:每个链接指向一个路由,如`/image/1`,该路由将显示一张网络图片。
实现思路:
- 主页:在index.html中,使用两个<img>标签,并通过CSS将它们重叠在一起(使用绝对定位)。
- 底部添加5个链接,分别指向5个不同的图片显示页面(每个页面显示一张网络图片)。
注意:为了简单,我们不在主页上显示五张图片,而是显示五个链接,点击链接跳转到新页面显示一张图片。
我们将创建以下路由:
- `/`:主页,显示叠加图片和5个链接。
- `/image/<int:image_id>`:根据image_id显示对应的网络图片。
由于我们使用网络图片,所以不需要在本地存储图片,只需要在HTML中直接使用图片URL。
我们预先准备5张网络图片的URL,存储在一个列表中。在`/image/<image_id>`路由中,根据image_id(1-5)来获取对应的URL并传递给模板。
主页叠加的两张图片,我们也可以使用两个固定的网络图片URL。
开始编写代码:
1. app.py</think>下面我将使用 Python 的 Flask 框架创建一个网站,实现主页显示两张叠加图片(一张在另一张上面),底部有5个链接,点击每个链接显示不同的网络图片。
### 解决方案
```python
from flask import Flask, render_template
app = Flask(__name__)
# 主页图片URL(两张叠加图片)
home_images = [
"https://picsum.photos/id/10/800/400", # 底部图片
"https://picsum.photos/id/20/400/200" # 顶部图片(较小)
]
# 链接对应的网络图片URL
link_images = {
1: "https://picsum.photos/id/100/800/600",
2: "https://picsum.photos/id/200/800/600",
3: "https://picsum.photos/id/300/800/600",
4: "https://picsum.photos/id/400/800/600",
5: "https://picsum.photos/id/500/800/600"
}
@app.route('/')
def home():
""" 主页:显示两张叠加图片和5个链接 """
return render_template('index.html',
bottom_img=home_images[0],
top_img=home_images[1],
links=link_images)
@app.route('/image/<int:img_id>')
def show_image(img_id):
""" 显示单个图片页面 """
image_url = link_images.get(img_id)
if not image_url:
return "Image not found", 404
return render_template('image.html', image_url=image_url)
if __name__ == '__main__':
app.run(debug=True)
```
### HTML 模板文件
创建 `templates/index.html`:
```html
<!DOCTYPE html>
<html>
<head>
<title>图片展示网站</title>
<style>
.container {
position: relative;
width: 800px;
margin: 0 auto;
}
.bottom-img {
width: 100%;
}
.top-img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
border: 3px solid white;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
.links {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 30px;
}
.links a {
padding: 10px 20px;
background: #4CAF50;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
}
.links a:hover {
background: #45a049;
}
</style>
</head>
<body>
<div class="container">
<!-- 底部图片 -->
<img src="{{ bottom_img }}" alt="背景图片" class="bottom-img">
<!-- 顶部图片 -->
<img src="{{ top_img }}" alt="前景图片" class="top-img">
</div>
<div class="links">
<!-- 动态生成5个链接 -->
{% for id, url in links.items() %}
<a href="/image/{{ id }}">图片 {{ id }}</a>
{% endfor %}
</div>
</body>
</html>
```
创建 `templates/image.html`:
```html
<!DOCTYPE html>
<html>
<head>
<title>图片详情</title>
<style>
body {
text-align: center;
padding: 20px;
}
.image-container img {
max-width: 90%;
max-height: 80vh;
border: 1px solid #ddd;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.back-link {
display: inline-block;
margin-top: 20px;
padding: 10px 20px;
background: #2196F3;
color: white;
text-decoration: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="image-container">
<img src="{{ image_url }}" alt="展示图片">
</div>
<a href="/" class="back-link">返回主页</a>
</body>
</html>
```
### 代码解释
1. **Flask 应用结构**:
- 使用 `home()` 函数处理主页请求
- 使用 `show_image()` 函数处理图片详情页请求
- 图片 URL 使用 https://picsum.photos 的随机图片服务
2. **主页功能**:
- 通过 CSS 绝对定位实现图片叠加效果
- 底部图片设为容器宽度的 100%
- 顶部图片居中显示,并添加白色边框和阴影
- 使用 Flexbox 布局创建等间距的链接按钮
3. **图片详情页**:
- 显示大尺寸图片(800x600)
- 包含返回主页的链接
- 响应式设计确保图片适应不同屏幕
4. **动态路由**:
- `/image/<int:img_id>` 根据链接 ID 动态加载对应图片
- 使用字典存储图片 URL,便于扩展
### 运行步骤
1. 创建项目文件夹并进入:
```bash
mkdir image_website && cd image_website
```
2. 创建 Python 文件 `app.py` 和模板文件夹:
```bash
touch app.py
mkdir templates
```
3. 将上述 HTML 代码保存到 `templates` 目录中
4. 安装依赖:
```bash
pip install flask
```
5. 运行应用:
```bash
python app.py
```
6. 在浏览器访问:http://127.0.0.1:5000
### 效果预览
- 主页:底部显示大图,顶部居中显示小图,下方有5个链接按钮
- 点击任意链接:跳转到对应图片的全屏展示页
- 每个页面都有返回主页的按钮
阅读全文
相关推荐




















