获取Flask错误获取html 404

2024-09-28 22:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

所有我的.html文件都存在于templates文件夹中

我在一个名为tour1.html的特定文件上遇到404错误

我将其路线定义为:

@app.route('/tour1.html')
def tour1():
    return render_template('tour1.html')

完全错误是

"GET tour1.html HTTP/1.1" 404

此外,当我在VS Code Live Server上运行时,链接工作正常,因此html文件中没有问题

下面是项目树的外观

myproject\
      Data\
      fonts\
      GraphQL\
      js\
      Model\
      MongoDB\
      static\
            css\
            img\
      templates\
            404.html
            about.html
            contact.html
            aruserinterface.html
            index.html
            tour1.html
      main.py

            

这是main.py中的代码

from MongoDB.script import Mongodb
from MongoDB.mongo import query
from flask_pymongo import PyMongo
from flask import Flask, jsonify, request, redirect, render_template, url_for, send_from_directory
from geopy.geocoders import Nominatim
import json, bson
import folium

mongoDb = Mongodb()

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {'db':'Cluster0', 'alias':'default'}

@app.route('/')
def home():
    return render_template('index.html')
@app.route('/about.html')
def about():
    return render_template('about.html')

@app.route('/contact.html')
def contact():
    return render_template('contact.html')
@app.route('/aruserinterface.html')
def aruserinterface():
    return render_template('aruserinterface.html')

@app.route('/tour1')
def tour1():
    return render_template('tour1.html')

@app.errorhandler(404)
def not_found(e):
  return render_template("404.html")
    
if __name__ == '__main__':
     app.run('127.0.0.1', port=5100)


Tags: 文件fromimportappreturnmongodbdefhtml
2条回答

我不认为.html需要成为路由的一部分,请检查这是否有效:

@app.route('/tour1')  # removed the .html from the URL route definition
def tour1():
    return render_template('tour1.html')

如果仍然出现404错误,则应使用如下curl命令测试路由:

curl -X GET "http://127.0.0.1:5100/tour1"

或者像Postman这样更专业的工具

我认为您正在url中执行tour1.html,但您的路线只是tour1

如果您正在这样做:

@app.route('/tour1')
def tour1():
    return render_template('tour1.html')

然后您必须在url中执行/tour1,而不是tour1.html

如果要在url中执行/tour1.html,则应执行以下操作:

@app.route('/tour1.html')
def tour1():
    return render_template('tour1.html')

相关问题 更多 >