烧瓶路由功能产生错误-“内部服务器错误”

时间:2019-10-19 06:56:58

标签: python flask jinja2 python-3.8

由于某种原因,每当我添加url_for()时,都会不断在index.html文件上检测到错误。无论我是否仅将函数添加到index.html,仅将more.html或同时添加到这两者,都会发生这种情况。

More.html似乎可以毫无问题地接受该功能。

该代码仅适用于第二种方法。将url_for()添加到代码后,我无法使第一个方法正常工作。

app.py:

import datetime

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    now = datetime.datetime.now()
    new_year = now.month == 1 and now.day == 1
    return render_template("index.html", new_year=new_year)

@app.route('/<string:name>')
def more(name):
    name = name.capitalize()
    defaultNames = ["Albert", "Michelle", "Ivette", "Sylvia"]
    return render_template("more.html", name=name, defaultNames=defaultNames)

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Python Tutorial</title>
</head>
<body>
    {% if new_year %}
        <h1>It's NEW year's</h1>
    {% else %}
        <h1>NOT NEW YEAR'S</h1>
    {% endif %}

    <a href="{{ url_for('more') }}">names page</a>
</body>
</html>

more.html:

<!DOCTYPE html>
<html>
<head>
    <title>names page</title>
</head>
<body>
    <h1>hello, {{name}}!</h1>

    <ul>
        {% for name in defaultNames %}
            <li>{{ name }}</li>
        {% endfor %}
    </ul>

    <a href="{{ url_for('index') }}">Go back</a>
</body>
</html>

每当我尝试使用url_for()函数运行代码时,都会看到“内部服务器错误”。第二种方法可以正常工作。

0 个答案:

没有答案