在 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

相关推荐
bagadesu几秒前
15.<Spring Boot 日志>
java·后端
9ilk18 分钟前
【基于one-loop-per-thread的高并发服务器】--- 项目测试
运维·服务器·c++·后端·中间件
白衣鸽子29 分钟前
ArrayUtils:Java数组操作的瑞士军刀
后端·开源·设计
听风吟丶34 分钟前
Java 函数式编程深度实战:从 Lambda 到 Stream API 的工程化落地
开发语言·python
舒一笑36 分钟前
PandaCoder:我的个人开发者工具进化之路
后端·程序员·intellij idea
IT_陈寒1 小时前
Vue 3.4 正式发布:5个不可错过的性能优化与Composition API新特性
前端·人工智能·后端
顾安r1 小时前
11.15 脚本算法 加密网页
服务器·算法·flask·html·同态加密
q***72191 小时前
Spring Boot环境配置
java·spring boot·后端
饮长安千年月2 小时前
玄机-第八章 内存马分析-java03-fastjson
开发语言·python·安全·web安全·网络安全·应急响应
天天爱吃肉82182 小时前
新能源汽车动力系统在环(HIL)半实物仿真测试台架深度解析
人工智能·python·嵌入式硬件·汽车