标题:关于用flask+flask_whooshalchemyplus全文检索问题
取消只看楼主
coffeeDj
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-5-23
 问题点数:0 回复次数:0 
关于用flask+flask_whooshalchemyplus全文检索问题
最近学校老师让我们自己学习搜索引擎用python语言的,自己网上查了很多,但还是有问题。如下
程序代码:
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)


# 配置 sqlalchemy  数据库驱动://数据库用户名:密码@主机地址:端口/数据库?编码
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:123456@localhost:3306/BYSJ?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['WHOOSH_BASE'] = 'path/to/whoosh/base'

# 初始化
db = SQLAlchemy(app)


models.py
from jieba.analyse.analyzer import ChineseAnalyzer
import flask_whooshalchemyplus
from __init__ import db, app


class filemessage(db.Model):
    __tablename__ = 'filemessage'
    __searchable__ = ['title', 'content']
    __analyzer__ = ChineseAnalyzer()
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Unicode)
    content = db.Column(db.Text)


flask_whooshalchemyplus.init_app(app)


view.py
from flask import render_template, request
from flask_bootstrap import Bootstrap
from flask_whooshalchemyplus import index_all
from __init__ import *
from models import filemessage
app = Flask(__name__)
bootstrap = Bootstrap(app)


@app.route('/')
def home():
    index_all(app)
    return render_template('search_results.html')


@app.route('/search', methods=['GET'])
def search_results():
    query = request.args.get('search')
    results = filemessage.query.whoosh_search(query).all()
    return render_template('search_results.html', results=results)


if __name__ == '__main__':
    app.debug = True
    app.run()




#前端
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <form action="{{url_for('search')}}" method="get">
          <input type="text" placeholder="站内搜索" name="search" >
          <button type="submit">搜索</button>
      </form>
</div>
<div>
<ul>
    {% for post in results %}
    <li>
        <div>
            <div>
                {{ post.title }}
                {{ post.content }}
            </div>
        </div>
    </li>
    {% endfor %}
</ul>
</div>
</body>
</html>

错误如下:

拒绝访问path是什么意思,没有这个路径我记得可以自己创建的,本人刚学flask,希望知乎大佬老解读
搜索更多相关主题的帖子: from import app db div 
2018-05-24 00:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-487221-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.183318 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved