我创建了一个下拉菜单,用于从SQL数据库中按项目编号搜索零件。第一页加载了正确的部分,但随后的任何页面都会出现以下错误:
TypeError:show_compound()缺少1个必需的位置参数:“ search_string”
根据我在网上看到的内容,似乎可能需要使用*args
或将search_string
传递给模板,但是我不确定如何使用*args
或在哪里插入模板上的search_string
值。
@parts_database.route('/searchcompound', methods=['GET', 'POST'])
@login_required
def compounds_search():
form = ProjectSearch(request.form)
if form.validate_on_submit():
search_string = form.select.data.project_number
return show_compound(search_string)
return render_template('parts_database/search_compounds.html', form=form)
@parts_database.route('/showcompound', methods=['GET'])
@login_required
def show_compound(search_string):
page = request.args.get('page', 1, type=int)
pagination = PartsTable.query.filter_by(project_number=search_string).order_by(PartsTable.part_number).paginate(page, per_page=15, error_out=False)
compound = pagination.items
page_10 = pagination.next_num+9
if page_10 > pagination.pages:
pageincrement = pagination.pages
else:
pageincrement = page_10
page_decrement = page - 10
if page_decrement < 1:
page_decrement = 1
return render_template('parts_database/showpartstable.html', compound=compound, pagination=pagination, pageincrement=pageincrement, page_decrement=page_decrement)
模板:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}
{% block title %}Amos{% endblock %}
{% block page_content %}
<div class="page-header">
<a href="{{ url_for('main.render_csv', jobname_filename = 'Parts_PDB') }}"><h1>Parts</h1></a>
{% include 'parts_database/_showpartstable.html' %}
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(page_decrement, pageincrement, pagination, '.show_compound') }}
</div>
{% endif %}
</div>
{% endblock %}
答案 0 :(得分:1)
如果视图带有参数,则必须在路由中包含该变量。
在您的情况下,您在search_string
路线定义中缺少show_compound
。尝试这样的事情:
@parts_database.route('/showcompound/<search_string>', methods=['GET'])
@login_required
def show_compound(search_string):
(...)
编辑:
此外,我很乐意重定向,而不是调用另一个视图的函数。
替换此:
if form.validate_on_submit():
search_string = form.select.data.project_number
return show_compound(search_string)
与此:
在此之前,您必须导入重定向:
from flask import redirect # (Add this at the top)
(...)
if form.validate_on_submit():
search_string = form.select.data.project_number
return redirect(url_for('parts_database.show_compound', search_string=search_string))