【问题标题】:Getting Flask Error GET html 404 constantly不断收到烧瓶错误GET html 404
【发布时间】:2021-07-30 08:08:19
【问题描述】:

我的所有.html 文件都存在于模板文件夹中。

我在一个名为 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)

【问题讨论】:

  • 如果问题是关于flask,为什么要django标签?
  • 我怀疑tour1.html 文件是否在templates 文件夹中?请重新检查!
  • @Xitiz 是!刚刚也重新检查了
  • @Ram 因为我看到有人为 django 提出同样的问题,我认为任何有 django 知识的人也可以提供帮助 :)
  • 同时发布您的完整代码和文件夹结构。

标签: python html flask web


【解决方案1】:

我认为 .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

【讨论】:

  • 我不认为路由中的.html 是个问题!
  • 即使路由中存在.html,它也能正常工作。这只是一条路线。
  • 它是一个 404 错误,这意味着在服务器上找不到 url 或资源。 .html 在flask.palletsprojects.com/en/2.0.x/tutorial/views 的任何示例中都不是路线的一部分,所以即使“它有效”,也没有必要把它放在那里。
  • 我已经用 main.py 代码和项目文件结构更新了问题,看看你现在能指出问题吗? @c8999c3f964f64
  • @c8999c3f964f64 我将 html 链接从 location.href('../templates/tour1.html') 更新为 location.href('tour1.html') 并且它以某种方式工作。现在正在呈现所有链接。非常感谢!
【解决方案2】:

我认为您在 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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2023-03-11
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多