简体   繁体   English

为什么 Flask 让我把 @app.route("/interior.html") 放在 route 而不是 @app.route("/interior")

[英]Why is Flask making me put @app.route("/interior.html") in route instead of @app.route("/interior")

I am building a website with a booking page that requires me to put the data into a database so I am using python Flask app.我正在构建一个带有预订页面的网站,该页面要求我将数据放入数据库中,因此我使用的是 python Flask 应用程序。

I know that in the @app.route I am only supposed to put @app.route("/exterior") however, whenever I try it using this method I get 404 Page Not Found.我知道在 @app.route 中我只应该放置 @app.route("/exterior") 但是,每当我使用这种方法尝试它时,我都会收到 404 Page Not Found。 Instead I have to put @app.route("/exterior.html").相反,我必须放置 @app.route("/exterior.html")。 This is the only way it will work.这是唯一可行的方法。 I believe I have all the correct libraries and I have defined everything correctly;我相信我拥有所有正确的库,并且我已经正确定义了所有内容; but, it only works if I put.html in the @app.route.但是,它只有在我将 .html 放入 @app.route 时才有效。

I have researched @app.routes and it only tells me the correct method which I know is @app.route("/exterior");我研究了@app.routes,它只告诉我我知道的正确方法是@app.route("/exterior"); however, the only thing that works is @app.route("/exterior.html").然而,唯一有效的是@app.route("/exterior.html")。 If anyone can tell me why this is happening that would be appreciated.如果有人能告诉我为什么会这样,我将不胜感激。

Here is my code.这是我的代码。

import os

from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from datetime import datetime

# Configure application - turn this file into a Flask application -
app = Flask(__name__)

# Ensure templates are auto-reloaded -
app.config["TEMPLATES_AUTO_RELOAD"] = True

# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///bookings.db")


@app.after_request
def after_request(response):
    """Ensure responses aren't cached"""
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

@app.route("/")
def index():
    return render_template("/index.html")

@app.route("/index.html")
def indexpage():
    return render_template("/index.html")

@app.route("/exterior.html")
def exterior():
    return render_template("exterior.html")


@app.route("/interior")
def interior():
    return render_template("interior.html")

if __name__ == 'main':
    app.run(debug = True)

As you can see this is resulting me having to use two routes to load my index page.如您所见,这导致我不得不使用两条路线来加载我的索引页面。 Once for when I initially run flask and again, so I am able to link to the pages in my navbar.一次是在我最初运行 flask 时再次运行,这样我就可以链接到导航栏中的页面。

It's also not the correct method and it bothers me that I am not able to do it correctly.这也不是正确的方法,令我困扰的是我无法正确地做到这一点。 Please advise.请指教。

Here is my file directory:这是我的文件目录:

project
    static
        pics
        styles.css
    templates
        index.html
        interior.html
        exterior.html
        about.html
        gallery.html
        layout.html
        booknow.html
    app.py
    bookings.db
    README.md

unsure why app.py and bookings are not before static and templates and alphabetically that would make more sense;不确定为什么 app.py 和 bookings 不在 static 之前,模板和字母顺序更有意义; however, this is how it is displayed.但是,这就是它的显示方式。

I copied your code and made some minor syntax fixes.我复制了您的代码并进行了一些小的语法修复。 below is working as normal.下面是正常工作。 perhaps you could copy this and start adding back some of your functionality and see where it goes wrong.也许您可以复制它并开始添加一些您的功能并查看哪里出了问题。

here is the directory structure:这是目录结构:

test_app/
    app.py
    templates/
        interior.html
        exterior.html
        index.html

and here is app.py:这是 app.py:

import os

# from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from datetime import datetime

# Configure application - turn this file into a Flask application -
app = Flask(__name__)

# Ensure templates are auto-reloaded -
app.config["TEMPLATES_AUTO_RELOAD"] = True

# Configure CS50 Library to use SQLite database
# db = SQL("sqlite:///bookings.db")


@app.after_request
def after_request(response):
    """Ensure responses aren't cached"""
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/index/")
def indexpage():
    return render_template("index.html")

@app.route("/exterior/")
def exterior():
    return render_template("exterior.html")


@app.route("/interior/")
def interior():
    return render_template("interior.html")

if __name__ == '__main__':
    app.run(debug = True)

this work normally for me.这对我来说很正常。 what about you?你呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM