很抱歉问这个问题。我确实找到了一些类似的问题,但是我花了2个小时来阅读它们,但我仍然无法弄清楚……我真的很不擅长编码,希望您能帮助我解决此错误:
“ c.execute(” SELECT * FROM blog_post“,('%'+ str(search)+'%',))
sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用0,并且提供了1。”
我创建了一个名为Blogsearch_form的form.py,以便用户可以通过index.html在我的网站上搜索博客。然后,我将其与我的views.py连接,最后与HTML文件连接。
我的views.py代码:
@core.route('/', methods=['GET', 'POST'])
def index():
# Call a function to later use in creating the template
search = Blogsearch_form(request.form)
if request.method == 'POST':
c.execute("SELECT * FROM blog_post", ('%' + str(search) + '%',))
results = c.fetchall()
return render_template('blog_search_result.html', results=results)
page = request.args.get('page',1,type=int)
many_posts = BlogPost.query.order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)
return render_template('index.html', many_posts=many_posts, form=search)
我的forms.py代码:
from wtforms import Form, StringField
class Blogsearch_form(Form):
search = StringField('')
我的blog_search_result.html代码:
{% extends "base.html" %}
{% block content %}
<h5>{{ results }}</h5>
{% endblock %}
我index.html中的相关代码:
{% from "formhelpers.html" import render_field %}
<form class="form-inline my-2 my-lg-0 text-right" method=POST>
{{ render_field(form.search) }}
<input type=submit value=Search>
</form>
非常感谢您阅读我的帮助请求!