在 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.htmltemplates/ 目录下

app.py 里添加 /introduce 路由

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

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

相关推荐
伊布拉西莫4 分钟前
LangChain LCEL源码深度剖析
python·langchain
用心_承载未来5 分钟前
从“复制链接→打开APP“到“一键解析“:我做了个短视频去水印工具
python·去水印·短视频去水印
TYUT_xiaoming7 分钟前
yolo模型训练
人工智能·python·yolo
雨辰AI24 分钟前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
Solis44 分钟前
Raft:分布式系统的定海神针
后端·架构
程序员老申1 小时前
第三篇 5 天 12 个 commit:踩坑实录与代码演进
后端·程序员
程序员鱼皮1 小时前
提示词工程已死,Loop Engineering 称王!保姆级教程 + 项目实战
前端·后端·ai编程
MageGojo1 小时前
百度热搜API接入实战:数据结构解析与工程化调用指南
python·数据抓取·api集成·热点数据·接口调试
TechWayfarer1 小时前
查IP归属地接入实战:保险理赔如何做动态风险监控与预警
网络·python·tcp/ip·安全·flask
Mininglamp_27181 小时前
Vibe Coding 之后是 Vibe Operating?
后端·开源·多智能体·ai agent·mano-p