【问题标题】:Flask Flatpages not able to load table of contentsFlask Flatpages 无法加载目录
【发布时间】:2020-07-24 19:15:44
【问题描述】:

我做了下面这个小网站作为对 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']?

我哪里错了?

【问题讨论】:

  • 在这一行&lt;a href="{{ url_for("page", page=page.path) }}" 1. 内部使用单引号,2. 我相信你的kwarg应该是path。更正后的行应类似于 &lt;a href="{{ url_for('page', path=page.path) }}" 看看是否适合您。

标签: python flask flask-flatpages


【解决方案1】:

正如上述作者所说,更改目录代码是可行的:

<a href="{{ url_for('page', path=page.path) }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多