网页打开错误TemplateNotFound

来源:1-1 Flask模板介绍

weixin_慕哥3021856

2023-06-17 15:55:54

问题描述:

代码感觉都没问题,服务也正常开启,但是自定义的html却无法访问?

相关代码:

from flask import Flask, current_app, make_response, render_template

app = Flask(__name__)

@app.route('/index')
def index():
    print(app)
    print(current_app)
    return 'index'

@app.route('/response')
def test_response():
    # return 'response successfully', 404, {
    #     'user_id': 'myuserid'
    # }

    response_obj = make_response('响应对象', 404, {
        'token': 'abcdef'
    })
    response_obj.headers['user_id'] = 'qwert'
    response_obj.status_code = 200
    return response_obj

@app.route('/html')
def test_html():
    """ 从文件中响应html """
    html = render_template("index.html")
    return html

相关截图:

https://img.mukewang.com/climg/648d666309eb83ac25601504.jpg

https://img.mukewang.com/climg/648d669809aab34325601504.jpg

https://img.mukewang.com/climg/648d66e7095853f025582464.jpg

写回答

1回答

好帮手慕小猿

2023-06-19

同学,你好!flask 模板默认是在templates文件夹下的,要把html文件放在该文件夹下。同学新建个文件夹名为templates ,再将index.html 放入到templates文件夹内,再重启服务,尝试重新访问。

https://img.mukewang.com/climg/648fc4b00920c18d03380113.jpg

祝学习愉快~

0

0 学习 · 4469 问题

查看课程