在 Flask 项目中访问其他页面

🚀 在 Flask 项目中访问 introduce.html

目前你只能访问 http://localhost:8080/index.html,但你希望可以访问 introduce.html。


✅ 1. 确保 introduce.html 存放在 templates/ 目录

Flask 默认会在 templates/ 目录下寻找 HTML 文件 。请确保 introduce.html 在 Flask 项目的 templates/ 目录下:

复制代码
your_flask_project/
│── app.py
│── templates/
│   │── index.html
│   │── introduce.html  <-- 确保这个文件存在
│── static/
│   │── css/
│   │── js/
│   │── imgs/

✅ 2. 在 app.py 里添加路由

在 app.py 里定义访问 introduce.html 的路由:

python 复制代码
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/introduce')
def introduce():
    return render_template('introduce.html')

if __name__ == '__main__':
    app.run(debug=True, port=8080)

📌 这样你可以通过 http://localhost:8080/introduce 访问 introduce.html


✅ 3. 让 index.html 里添加 introduce.html 的链接

如果你想在首页 index.html 里添加跳转到 introduce.html 的按钮,可以在 index.html 里这样写:

html 复制代码
<a href="{{ url_for('introduce') }}" class="btn">了解更多</a>

📌 这样点击按钮就能跳转到 introduce.html


✅ 4. 运行 Flask 并测试

在终端运行:

bash 复制代码
python app.py

然后访问:

  • 首页 :http://localhost:8080/
  • 功能介绍页面 :http://localhost:8080/introduce

🎯 结论

✅ 确保 introduce.html 在 templates/ 目录下

✅ 在 app.py 里添加 /introduce 路由

✅ 在 index.html 里用 <a href="{``{ url_for('introduce') }}"> 进行跳转

✅ 成功访问 http://localhost:8080/introduce

相关推荐
青少儿编程课堂14 分钟前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
Wellijohn15 分钟前
Python 异常到底多贵?我把 try、raise、traceback 的账单逐项拆开测了一遍
后端·python·性能优化
考虑考虑24 分钟前
Spingboot4.2.0中的属性
spring boot·后端·spring
Dmz丶1 小时前
前端转 AI 100 天 Day 55:RAG 项目实战(四)——前端完善,做一个能用的产品
python
JAVA前线2 小时前
矛盾不会消失只会转移:AI时代的项目管理探讨
后端·团队管理
geovindu2 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
mattleeee2 小时前
Python脚本超时重试机制设计:失败立即重试,最多3次
python·超时·重试机制
笨鸟先飞的橘猫2 小时前
SLG 后端实战(一):基础建模与行军模拟
python·游戏程序
Thanks_ks4 小时前
接口限流是怎么实现的:从固定窗口计数讲到滑动窗口日志
redis·python·限流·滑动窗口·漏桶·令牌桶·固定窗口