Flask url_for无法基于工作代码构建端点。问我是否要索引?

时间:2018-09-05 15:27:29

标签: flask jinja2

我的Flask应用已经提供了多个页面。我刚刚做了channels.html。对于路由,我复制并更改了/messages,该路由已经有效。然后,我尝试添加从messages.htmlchannels.html的链接,但收到错误:Could not build url for endpoint 'channels'. Did you mean 'index' instead?该错误引用了<a href="{{ url_for('channels') }}">Channels</a>中的行messages.html。我检查了拼写,语法,修饰符的位置和重复的名称;一切看起来还好。另外,新代码是基于先前测试过的代码构建的。

那么,为什么会出错?

@app.route('/channels', methods=["GET", "POST"])
def channels():
    return render_template("channels.html")

@app.route('/messages', methods=["GET", "POST"])
def messages():
    return render_template("messages.html")

messages.html(顶部)

{% extends "layout.html" %}

{% block body %}
<div class="container">
    <div class="row"  style="min-height: 100vh">
        <div class="col-md-3 border border-danger rounded">
            <div class="row mt-2 justify-content-start">
                Header
            </div>
            <!-- <div class="row mt-2 justify-content-start" id="channelHeader">
                Channels
            </div>-->
            <div class="row mt-2 justify-content-start" id="channels">
                <a href="{{ url_for('channels') }}">Channels</a>
            </div>
            &nbsp;
            <div class="row justify-content-start" id="dmsg">
                Direct Messages
            </div>
        </div>
        <div class="col-md-9 border border-danger rounded">
{% endblock %}

channels.html

{% extends "layout.html" %}

{% block body %}

<div class="container">
    <div class="display-3">
        <strong>Create A Channel</strong>
    </div>

    <form action="{{ url_for('channels') }}" id="channelForm" class="mt-4">
        <!-- Will need check for duplicate email -->
      <div class="form-group">
        <label for="channelName">Channel Name</label>
        <input type="text" class="form-control" id="channelName" aria-describedby="channelName" placeholder="Enter Channel Name">
      </div>
      <!-- Will need check for duplicate username -->
      <div class="form-group">
        <label for="inviteUsers">Invite Other Users (optional)</label>
        <input type="text" class="form-control" id="inviteUsers" aria-describedby="inviteUsers" placeholder="Search by name">
      </div>
      <div class="form-group">
        <label for="purpose">Purpose</label>
        <textarea type="text" class="form-control" id="purpose" rows="3" placeholder="Purpose">
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>

{% endblock %}

0 个答案:

没有答案