jinja2.exceptions.TemplateSyntaxError:遇到未知标签“ url”

时间:2019-10-03 03:16:47

标签: flask jinja2

这听起来可能是一个多余的问题,但是以前的解决方案都无法满足我的要求。我是新来的烧瓶。可能是隐藏在我看不见的东西中的东西。请帮帮我。

在我的基本模板(baseLayout.html)中,添加了一个导航栏(使用bootstrap样式)。直到我将href更新为-

<li class="nav-item ml-md-5">
    <a href="{% url 'taskManager-home' %}" class="nav-link">Home</a>

它向我显示了大量错误语句,但最后一个是-

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'url'.

请在这里帮助我。

1 个答案:

答案 0 :(得分:0)

要从Flask方法获取URL,应在模板中使用url_forurl_for的详细文档可以在official documentation中找到。

在这里,我展示了一个使用Bootstrap Navbar和url_for进行Flask动态路由的示例。

目录结构:

.
├── app.py
├── requirements.txt
└── templates
    └── index.html

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def view_home():
    return render_template("index.html", title="Home page")

@app.route("/first")
def view_first_page():
    return render_template("index.html", title="First page")

@app.route("/second")
def view_second_page():
    return render_template("index.html", title="Second page")

index.html

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Navbar Using Flask</title>
</head>

<body>

    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="#">Navbar</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="{{ url_for('view_home') }}">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('view_first_page') }}">First Page</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('view_second_page') }}">Second Page</a>
                    </li>
                </ul>
            </div>
        </nav>

        <div class="jumbotron">
            <h1 class="display-4">
        {% if title %}
          {{ title }}
        {% else %}
        Default title
        {% endif %}
        </h1>
        </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

截屏:

  1. 主页:

home page

  1. 第二页:

second page

已安装的软件包(requirements.txt):

Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.16.0