我有一个问题。我正在使用flask创建搜索结果。它工作正常,但是当我尝试 使用页面导航导航到下一页,它显示错误:
'TypeError:视图函数未返回有效响应。该函数返回None或不返回return语句而结束。我究竟做错了什么 ?代码如下所示:
@main.route("/filter", methods=['GET', 'POST'])
def filter():
if request.method == 'GET':
#page = request.args.get('page', 1, type=int)
search_value = request.args.get('search')
filter0 = "%{0}%".format(search_value)
filter1 = request.args.get('myselect')
filter2 = request.args.get('myselect2')
if filter1 == 'new':
if filter2 == "All":
page = request.args.get('page', 1, type=int)
posts = Post.query.filter(Post.course_name.like(filter0)).filter_by().order_by(Post.timestamp.desc()).paginate(page=page, per_page=10)
return render_template('home.html', posts=posts, filter1 = filter1, filter2 = filter2)
{% for page_num in posts.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
{% if page_num %}
{% if posts.page == page_num %}
<a class="btn btn-info mb-4" href="{{ url_for('main.filter', page=page_num) }}">{{ page_num }}</a>
{% else %}
<a class="btn btn-outline-info mb-4" href="{{ url_for('main.filter', page=page_num) }}">{{ page_num }}</a>
{% endif %}
{% else %}
...
{% endif %}
{% endfor %}
答案 0 :(得分:1)
if filter1 != 'new'
,视图函数什么也不返回,
以下代码可以解决此问题
@main.route("/filter", methods=['GET', 'POST'])
def filter():
if request.method == 'GET':
pass
if filter1 == 'new':
if filter2 == "All":
pass
return render_template('home.html', posts=posts, filter1 = filter1, filter2 = filter2)
return 'something'