Flask BuildError-无法为端点建立URL

时间:2019-10-13 04:26:14

标签: python html flask

我已经有了一个非常简单的python代码,可以从数据库中选择一些数据

upload

但是当我渲染HTML页面时,我得到了 werkzeug.routing.BuildError:无法为端点'studentsearch'构建url。您是说“新闻学生”吗?

我的桌子也很简单...

@app.route('/studentsearch', methods = ['POST', 'GET'])
def searchstudent():
    """ Displays the student search page for the site """
    cursor = mydb.cursor()
    cursor.execute ("SELECT * FROM student")
    all_students = cursor.fetchall()
    cursor.close()
    return render_template('studentsearch.html', all_students = all_students)

这是怎么回事?在python或HTML中甚至没有提到“ newstudent”?

根据要求,以下是“ newstudent”似乎来自的代码:

<table>
    <thread>
        <tr>
            <th>Student ID:</th>
            <th>First Name:</th>
            <th>Last Name:</th>
            <th>Click to View Student Details</th>
        </tr>
    </thread>
    <tbody>
        {% for each_result in all_students %}
            <tr>
                <td>{{ each_result[0] }}</td>
                <td>{{ each_result[1] }}</td>
                <td>{{ each_result[2] }}</td>
                <td>CLICK HERE!!!</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

您是否要使用 url_for 函数在应用程序代码或HTML代码中的某个位置构建动态URL?

如果您愿意,那么我认为可能正在发生的是,您正在将参数'studentsearch'传递给该函数,而不是'searchstudent' url_for 使用视图函数名称(而不是端点名称)构建端点)。例如。在HTML模板中,所涉及的URL的构建方式如下:

<a href="{{ url_for('searchstudent') }}">Search</a>