zoukankan      html  css  js  c++  java
  • flask+sqlachemy 查询(转)

    main.py

    1# coding: utf-8
    from sqlalchemy import CheckConstraint, Column, Integer, Text, or_
    from sqlalchemy.schema import FetchedValue
    from flask_sqlalchemy import SQLAlchemy
    from flask import Flask,url_for,redirect,render_template,request,flash
    import os,sys
    
    app =  Flask(__name__)
    basedir = os.path.abspath(os.path.dirname(__file__))
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    
    db = SQLAlchemy(app)
    
    class Teacher(db.Model):
        __tablename__ = 'Teachers'
        Id = db.Column(db.Integer, primary_key=True)
        Name = db.Column(db.Text)
        Age = db.Column(db.Integer)
        Country = db.Column(db.Text)
    
        def __init__(self,Name,Age,Country):
            self.Name = Name
            self.Age = Age
            self.Country = Country
    @app.route('/', methods=['GET'])
    def home():
        article = Teacher.query.all()
        return render_template('show_article.html',article=article)    
    @app.route('/search')
    def search():
        keyword = request.args.get('keyword')
        print(keyword)
        result = Teacher.query.filter(or_(Teacher.Name==keyword,Teacher.Age==keyword,Teacher.Country==keyword)).all() 
        if result:
            return render_template('show_article.html', article=result)
        else:
            return 'No content was found!'
    if __name__ =='__main__':
        app.run(port=80)      
    

     

    show_article.html

    <!DOCTYPE HTML>
    <html>
    <head>
    <link href="//www.w3cschool.cn/statics/plugins/bootstrapold/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
    <!-- Jinja模板语法 -->
    <div>
        <form method="GET" action="{{ url_for('search') }}" />
            <input type="text" Name="keyword" />
            <input type="submit" />
            <span><a href="{{ url_for('home') }}">Home</a></span>
        </form>
        
    </div>
    <table style="240px" class="table table-striped" >
        <thead>
            <tr >
                <th style="120px">Name</th>
                <th style="60px">Age</th>
                <th style"60px">Country</th>
            </tr>
        </thead>
        <tbody>                 
            {% if article %}
                {% for article in article%}
                <tr>
                    <td>{{article.Name}}</td>   
                    <td>{{article.Age}}</td>   
                    <td>{{article.Country}}</td>
                </tr>
                {% endfor %}
        </tbody>
            {% else %}
                <p>No content was found!</p>
            {% endif %}
            
        
    </table>
    </body>
    </html>
    

      

  • 相关阅读:
    编程题练习
    算法:快速排序
    ansible的playbook剧本
    Django框架之验证码生成示例
    Django之auth模块用户认证模块
    Django框架之models和不依赖Qquery的ajax请求
    Django框架之序列化和上传文件
    Django高级篇一RESTful架构及API设计
    Git工具和GitHub的使用
    python之Flask框架
  • 原文地址:https://www.cnblogs.com/luoye00/p/12156848.html
Copyright © 2011-2022 走看看