在 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

相关推荐
仙人掌_lz19 分钟前
AI与机器学习ML:利用Python 从零实现神经网络
人工智能·python·机器学习
import_random40 分钟前
[macos]rocketmq(安装)
后端
逸雪飞扬1 小时前
Gradio 非侵入式修改的离线使用方案
python·html
程序员小假1 小时前
你会不会使用 SpringBoot 整合 Flowable 快速实现工作流呢?
java·后端
来自外太空的鱼-张小张1 小时前
java将pdf文件转换为图片工具类
java·python·pdf
endNone1 小时前
【机器学习】SAE(Sparse Autoencoders)稀疏自编码器
人工智能·python·深度学习·sae·autoencoder·稀疏自编码器
明月与玄武1 小时前
快速掌握Django框架设计思想(图解版)
后端·python·django
陪我一起学编程1 小时前
关于ORM增删改查的总结——跨表
数据库·后端·python·django·restful
旷野说1 小时前
cpp 绑定方案大比拼
python
Q_Q5110082852 小时前
python+django/flask成都奥科厨具厂产品在线销售系统
vue.js·spring boot·python·django·flask·node.js·php