0

我做了下面这个小网站作为对 Flask 的介绍,已经不行了!它无法加载目录页面。

我的app.py,即服务器如下:

import os
from flask import Flask, request, render_template
from flask_flatpages import FlatPages
from Engine.nerve_net import Nerve_Tree

# Some configuration, ensures:
# 1. Pages are loaded on request.
# 2. File name extension for pages is html.
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.html'

#Instantiate the Flask app
app = Flask(__name__)
app.config.from_object(__name__)
pages = FlatPages(app)

@app.route("/")
@app.route("/index")
def index():
    return render_template('index.html') #This returns the main welcome page

@app.route("/contents")
def contents():
    return render_template('contents.html', pages=pages) #This returns the table of contents page

# URL Routing - Flat Pages: Retrieves the page path
@app.route("/<path:path>/")
def page(path):
    page = pages.get_or_404(path)
    return render_template("page.html", page=page)

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

我的目录,显示pages目录中所有文件的页面如下:

{% extends "tab.html" %}

{% block content %}
</br>
<h2>TABLE OF CONTENTS</h2>
<ul>
{% for page in pages %}
    <li>
        <a href="{{ url_for("page", page=page.path) }}">{{ page.title }}
    </li>
{% else %}
    <li>No pages so far</li>
{% endfor %}
</ul>
{% endblock content %}

pages本例中名为 的目录中只有一页,ncs1.html如下:

title: Hello
published: 2010-12-22

Hello, *World*!

Lorem ipsum dolor sit amet

将浏览器指向我的主要欢迎页面http://127.0.0.1:5000/会成功呈现该页面。但是,将我的浏览器指向目录页面http://127.0.0.1:5000/contents会出现以下错误:

Could not build url for endpoint 'page' with values ['page']. Did you forget to specify values ['path']?

我哪里错了?

4

1 回答 1

0

正如上面提到的作者,更改目录代码有效:

<a href="{{ url_for('page', path=page.path) }}"
于 2020-07-26T02:14:22.380 回答