【问题标题】:Flask gives undefined error for url_for in html file when referencing to a blueprint pageFlask 在引用蓝图页面时为 html 文件中的 url_for 提供未定义的错误
【发布时间】:2019-11-05 09:01:30
【问题描述】:

我尝试构建一个 Web 应用程序,在导航菜单栏中,当我尝试打开 localhost/ 时,注册蓝图出现未定义错误(所以@app.route('/')base.html)有人可以帮我吗?

init.py

from flask import (
    Flask, render_template)
from .database import DB


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config["MONGO_URI"] = "mongodb://localhost:27017/chpalinka"
    DB.init()

    # a simple page that says hello
    @app.route('/')
    def home():
        return render_template('base.html')

    from . import jegyzokonyv
    app.register_blueprint(jegyzokonyv.bp)

    return app

jegyzokony.py

import functools

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from .database import DB


bp = Blueprint('jegyzokonyv', __name__, url_prefix='/jegyzokonyv')

@bp.route('/cefrezes', methods=('GET', 'POST'))
def cefrezes():
    jegyzokonyvek = DB.get_all('cefreze')
    return render_template('jegyzokonyv.html', query=jegyzokonyvek)

@bp.route('/fozes', methods=('GET', 'POST'))
def fozes():
    pass

@bp.route('/erleles', methods=('GET', 'POST'))
def erleles():
    pass

以及base.html的导航部分

<nav class="mt-2">
    <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
        <!-- Add icons to the links using the .nav-icon class
                 with font-awesome or any other icon font library -->
        <li class="nav-item">
            <a href="#" class="nav-link active">
                <i class="nav-icon fas fa-tachometer-alt"></i><p>Kezdőlap</p></a>
        </li>
        <li class="nav-header">Jegyzőkönyvek</li>
        <li class="nav-item"><a href="{{ url_for(jegyzokonyv.cefrezes) }}" class="nav-link">
            <i class="nav-icon fa fa-filter"></i><p>Cefrézés</p></a>
        </li>
        <li class="nav-item"><a href="{{ url_for(jegyzokonyv.fozes) }}" class="nav-link">
            <i class="nav-icon fa fa-tint"></i><p>Főzés</p></a>
        </li>
        <li class="nav-item"><a href="{{ url_for(jegyzokonyv.erleles) }}" class="nav-link">
            <i class="nav-icon fa fa-cogs"></i><p>Érlelés</p></a>
        </li>
    </ul>
</nav>

aa和错误消息:

  File "D:\WebPages\Palinka_Flask\flaskr\templates\base.html", line 183, in top-level template code
    <li class="nav-item"><a href="{{ url_for(jegyzokonyv.cefrezes) }}" class="nav-link">
  File "D:\WebPages\Palinka_Flask\venv\lib\site-packages\jinja2\environment.py", line 430, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'jegyzokonyv' is undefined

【问题讨论】:

    标签: flask undefined url-for


    【解决方案1】:

    url_for 接受一个字符串。将模板中的 url_for(jegyzokonyv.cefrezes) 更改为 url_for('jegyzokonyv.cefrezes')

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2020-05-13
      相关资源
      最近更新 更多