python Flask 扩展基本模板不起作用

问题描述 投票:0回答:1

我正在尝试为我的 Flask 应用程序使用模板,但由于某些原因,即使我在两个模板文件中指定了路径并包含 {% block content %}{% endblock content %} ,模板也无法工作。我很困惑为什么这行不通。这里出了什么问题?

/模板/base.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Class Project</title>
    <link rel="stylesheet" href="../style.css" type="text/css" media="screen" title="no title" charset="utf-8">
  </head>
  <body class="background-color">
    <div class="container">
      <nav>
        <a href="/">Display All Songs</a>
        <a href="/add-songs">Add Songs</a>
        <a href="/delete-songs">Delete Songs</a>
        <a href="/search-songs">Search Songs</a>
      </nav>
      <main>{% block content %}{% endblock content %}</main>
    </div>
  </body>
</html>

/模板/home.html

{% extends 'base.html' %}
{% block content %}{% endblock content %}
    {% for song in song_list%}
    <div>
      <p>Music Name: {{song["song_name"]}}</h1>
      <p>user: {{song["user_name"]}}</p>
    </div>
    <br>
    {% endfor %}

应用程序.py

import pymongo
from flask import Flask, request, render_template, redirect

app = Flask(__name__)


# Views
@app.route("/")
@app.route("/<user_name>")

def index():
    return render_template("home.html")

def display_songs(user_name=None):
    song_list = None
    if user_name:
        song_list = list(db.songs.find({"user_name": user_name}))
    else:
        song_list = list(db.songs.find({}))

    return render_template('home.html', song_list=song_list)


@app.route("/add-songs")
def add_songs():
    return render_template("addSongs.html")


@app.route("/delete-songs")
def delete_songs():
    return render_template("deleteSongs.html")


@app.route("/search-songs")
def search_songs():
    return render_template("searchSongs.html")


# Form methods
@app.route("/api/add-songs", methods=["POST"])
def addSongs():
    user_name = request.form["user_name"]
    song_name = request.form["song_name"]
    db.songs.insert_one({"user_name": user_name, "song_name": song_name})
    return redirect("/")


@app.route("/api/delete-songs", methods=["POST"])
def deleteSongs():
    user_name = request.form["user_name"]
    song_name = request.form["song_name"]
    db.songs.delete_one({"user_name": user_name, "song_name": song_name})
    return redirect("/")


@app.route("/api/search-songs", methods=["POST"])
def searchSongs():
    user_name = request.form["user_name"]
    return redirect("/" + user_name)


# Connecting to local host
connection = pymongo.MongoClient("mongodb://localhost:27017")
db = connection["test_database"]

python html templates flask web-applications
1个回答
0
投票
home.html 模板中的

{% block content %}{% endblock content %}

 旨在包含将注入到 base.html 的 
{% block content %}{% endblock content %}
 字符串中的 html 代码。您需要移动模板末尾的结束标签 
{% endblock content %}

home.html

{% extends 'base.html' %} {% block content %} {% for song in song_list%} <div> <p>Music Name: {{song["song_name"]}}</h1> <p>user: {{song["user_name"]}}</p> </div> <br> {% endfor %} {% endblock content %}
    
© www.soinside.com 2019 - 2024. All rights reserved.