码迷,mamicode.com
首页 > 其他好文 > 详细

flask_whooshalchemyplus 搜索

时间:2018-04-27 02:16:14      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:for   哈哈   man   url   font   rip   nes   token   运行   

  • 搞了半天没搞好, 不过最后还是搞好了,嘿嘿嘿, 美滋滋,来首 Good Time
  • 好了好了, 进入正题
  1. 删库跑路, 哈哈哈 假的, 我是直接把database删了, 然后再创
  2. 直接跑的话会报错, 比如像db, 还有manage.py啥的懒得写了
  3. models.py

    import flask_whooshalchemyplus
    from jieba.analyse import ChineseAnalyzer
    
    class Student(db.Model):
        __tablename__ = ‘student‘
        __searchable__ = [‘name‘, ‘uuid‘]
        __analyzer__ = ChineseAnalyzer()
        id = db.Column(db.Integer, primary_key=True)
        uuid = db.Column(db.String(255), unique=True)
        name = db.Column(db.String(100))      
    
    
    flask_whooshalchemyplus.init_app(app)
    
    if __name__ == ‘__main__‘:
        db.create_all()
  4. 运行models.py
  5. forms.py

    from flask_wtf import FlaskForm
    from wtforms import StringField, SubmitField
    from wtforms.validators import DataRequired
    
    
    class SearchForm(FlaskForm):
        key = StringField(
            label=‘关键词‘,
            description=‘关键词‘,
            validators=[
                DataRequired(‘请输入关键词!‘)
            ],
            render_kw={
                ‘placeholder‘: ‘请输入关键词‘
            }
        )
    
        submit = SubmitField(
            label=‘查询‘
        )
  6. views.py

    
    @home.route(‘/search/‘, methods=[‘POST‘, ‘GET‘])
    def search():
        form = SearchForm()
        if form.validate_on_submit():
            data = form.data
            return redirect(url_for(‘home.search_results‘, key=data[‘key‘]))
        return render_template(‘home/search.html‘, form=form)
    
    
    @home.route(‘/search_results/<string:key>‘)
    def search_results(key):
        results = Student.query.whoosh_search(key).all()
        return render_template(‘home/search_results.html‘, key=key, results=results, count=count)
  7. search.html

    <form method="post">
        {{ form.key }}
        {{ form.csrf_token }}
        {{ form.submit }}
    </form>
  8. search_results.html

    "{{ key }}"共有{{ count }}条
    <br>
    {% for i in results %}
        {{ i.name }}
        {{ i.uuid }}
        <br>
    {% endfor %}

dsds

flask_whooshalchemyplus 搜索

标签:for   哈哈   man   url   font   rip   nes   token   运行   

原文地址:https://www.cnblogs.com/edhg/p/8955273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!