我在使用Flask-WhooshAlchemy进行的文本搜索中也经历了类似的发布。但是,没有一个搜索提供具体的结果,也没有一个解决方案。
我的任务是使用flask和python从数据库中搜索文本。我的数据库是MySQL,我正在使用SQLAlchemy。
我的程序结构如下:
BlogPost
Admin
__init__.py
forms.py
views.py
User
__ init__.py
forms.py
views.py
__init__.py
models.py
初始化 .py:
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import flask_whooshalchemy as whooshalchemy
app.config['SECRET_KEY'] = 'mysecret'
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir,'app_v29.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['WHOOSE_BASE'] = 'sqlite:///'+os.path.join(basedir,'app_v29.db')
models.py
import flask_whooshalchemy as whooshalchemy
from BlogPost import app
class Task_Add(db.Model):
__searchable__ =['task_add']
id = db.Column(db.Integer,primary_key=True)
task_add = db.Column(db.String(64))
def __init__(self,task_add):
self.task_add = task_add
def __repr__(self):
return f"{self.task_add}"
whooshalchemy.whoosh_index(app, Task_Add)
class Invoice_Add(db.Model):
id = db.Column(db.Integer,primary_key=True)
invoice_add = db.Column(db.String(64))
def __init__(self,invoice_add):
self.invoice_add = invoice_add
def __repr__(self):
return f"{self.invoice_add}"
views.py
@adminDash.route('/view_task')
def view_task():
task_all=Task_Add.query.all()
return render_template('admin/view_task.html',task_all=task_all)
@adminDash.route('/search_task')
def search_task():
q=request.args.get('query')
task_all=Task_Add.query.whoosh_search(q).all()
return render_template('admin/view_task.html',task_all=task_all)
view_task.html
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="sidebar-module sidebar-module-insert">
<form class="form-inline" method="GET" action="{{url_for('adminDash.search_task')}}">
<div class="form-group">
<input type="text" class="form-control" name="query" id="query">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
</div>
</div>
<!-- .card -->
<section id="base-style" class="card">
<!-- .card-body -->
<div class="card-body">
<!-- .form -->
<div class="lits-group list-group-flush">
<!-- .lits-group-item -->
<!-- .table-responsive -->
<div class="table-responsive">
<!-- .table -->
<table class="table table-sm mb-0">
<!-- thead -->
<thead class="thead-">
<tr>
<th><font color="#0f1f94"> Task</font></th>
<th><font color="#0f1f94"> Delete</font></th>
</tr>
</thead>
<!-- /thead -->
<!-- tbody -->
<tbody>
<!-- tr -->
<tr>
{% for task_in in task_all %}
<td> {{task_in.task_add}} </td>
<td>
<form action="{{url_for('adminDash.delete_task',task_post_id=task_in.id)}}" method="POST">
<button type="submit" class="far fa-trash-alt"></button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- .lits-group-item -->
</div>
</div>
</div>
当我在搜索按钮中搜索查询时,它会将文本发送到/ search_task,但是
task_all=Task_Add.query.whoosh_search(q).all()
如果使用
进行查询,则会在结果中提供[]task_all=Task_Add.query.all()
它将提供查询。
谢谢您让我知道我在哪里犯错。以及如何在蓝图中使用Flask-WhooshAlchemy?
谢谢