![](/img/trans.png)
[英]Flask python - werkzeug.routing.BuildError: Could not build url for endpoint
[英]Flask BuildError - Could not build URL for endpoint
我有一個非常簡單的 python 代碼到 select 我的數據庫中的一些數據
@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)
但是當我 go 渲染 HTML 頁面時,我得到werkzeug.routing.BuildError: Could not build url for endpoint 'studentsearch'。 你指的是“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>
這是怎么回事? python 或 HTML 中甚至沒有提到“newstudent”?
根據要求,這里是“newstudent”似乎來自的代碼:
def newstudent():
"""Displays the new student page for the site"""
return render_template('newstudent.html')
@app.route('/process_newstudent', methods = ['POST', 'GET'])
def process_newstudent():
""" Processes the data from the new student course """
if request.method == 'POST':
first_name = request.form['firstname']
last_name = request.form['lastname']
year_began = request.form['yearbegan']
try:
cursor = mydb.cursor()
cursor.execute ("INSERT INTO student (first_name, last_name, year_began) VALUES ('{}','{}','{}');".format((first_name.title()), (last_name.title()), year_began))
mydb.commit()
cursor.close()
except mysql.connector.Error as err:
cursor.close()
return failure('newstudent', f"Error message: {err}. Student not added")
else:
cursor = mydb.cursor()
cursor.execute ("SELECT * FROM student")
student_success = cursor.fetchall()
cursor.close()
return render_template('newstudent_success.html', student_success = student_success)
else:
request.method == 'GET'
return render_template('newstudent.html')
您是否嘗試使用url_for function 在應用程序代碼或 HTML 代碼的某處構建動態 URL?
如果是,那么我認為可能發生的情況是您將參數'studentsearch'傳遞給 function 而不是'searchstudent' ( url_for使用視圖 function 名稱構建端點,而不是端點名稱)。 例如,在 HTML 模板中,有問題的 URL 將像這樣構建:
<a href="{{ url_for('searchstudent') }}">Search</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.