![](/img/trans.png)
[英]Flask: why app.route() decorator, should always be the outermost?
[英]Why is Flask making me put @app.route("/interior.html") in route instead of @app.route("/interior")
我正在構建一個帶有預訂頁面的網站,該頁面要求我將數據放入數據庫中,因此我使用的是 python Flask 應用程序。
我知道在 @app.route 中我只應該放置 @app.route("/exterior") 但是,每當我使用這種方法嘗試它時,我都會收到 404 Page Not Found。 相反,我必須放置 @app.route("/exterior.html")。 這是唯一可行的方法。 我相信我擁有所有正確的庫,並且我已經正確定義了所有內容; 但是,它只有在我將 .html 放入 @app.route 時才有效。
我研究了@app.routes,它只告訴我我知道的正確方法是@app.route("/exterior"); 然而,唯一有效的是@app.route("/exterior.html")。 如果有人能告訴我為什么會這樣,我將不勝感激。
這是我的代碼。
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)
如您所見,這導致我不得不使用兩條路線來加載我的索引頁面。 一次是在我最初運行 flask 時再次運行,這樣我就可以鏈接到導航欄中的頁面。
這也不是正確的方法,令我困擾的是我無法正確地做到這一點。 請指教。
這是我的文件目錄:
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
不確定為什么 app.py 和 bookings 不在 static 之前,模板和字母順序更有意義; 但是,這就是它的顯示方式。
我復制了您的代碼並進行了一些小的語法修復。 下面是正常工作。 也許您可以復制它並開始添加一些您的功能並查看哪里出了問題。
這是目錄結構:
test_app/
app.py
templates/
interior.html
exterior.html
index.html
這是 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)
這對我來說很正常。 你呢?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.