当前位置: 首页 > article >正文

在 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 的路由:

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 里这样写:

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

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


✅ 4. 运行 Flask 并测试

在终端运行:

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


http://www.kler.cn/a/580110.html

相关文章:

  • BGP实验(一)IBGP全互联配置
  • 解决asp.net mvc发布到iis下安全问题
  • javascript-es6 (六)
  • C语言八股---预处理,编译,汇编与链接篇
  • 【CSS3】金丹篇
  • 短分享-Flink图构建
  • 个人记录的一个插件,Unity-RuntimeMonitor
  • HAL库常用函数
  • 基于Asp.net的高校迎新管理系统
  • Linux Bash 单命令行解释 | 文件操作 / 字符串操作 / 重定向
  • 《云原生技术:DeepSeek分布式推理的效能倍增器》
  • 探索AI对冲基金:开源自动化交易系统的革新之路
  • 2025 ubuntu24系统宿主机上在线安装mysql数据库完整演示
  • docker无法pull镜像问题解决for win10
  • hcia华为路由器静态路由实验配置
  • 再聊 Flutter Riverpod ,注解模式下的 Riverpod 有什么特别之处,还有发展方向
  • 2025年渗透测试面试题总结-快某手-安全实习生(一面、二面)(题目+回答)
  • 【实战ES】实战 Elasticsearch:快速上手与深度实践-5.3.1GeoPoint与GeoShape的选型
  • FX-继承访问权限问题
  • 小程序 wxml 语法 —— 35 wxml 语法 -声明和绑定数据