首页 > 数据库技术 > 详细

flask显示数据库已有内容

时间:2019-12-27 12:48:12      阅读:87      评论:0      收藏:0      [点我收藏+]

main.py

1# coding: utf-8
from sqlalchemy import CheckConstraint, Column, Integer, Text
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy
from flask import Flask,url_for,redirect,render_template,request
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)      
if __name__ ==‘__main__‘:
    app.run(port=8000)  

 

show_article.html

<!DOCTYPE HTML>
<html>
<head>
<link href="//www.w3cschool.cn/statics/plugins/bootstrapold/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Jinja模板语法 -->
<table style="width:240px" class="table table-striped" >
    <thead>
        <tr >
            <th style="width:120px">Name</th>
            <th style="width:60px">Age</th>
            <th style"width: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>

  

  

flask显示数据库已有内容

原文:https://www.cnblogs.com/luoye00/p/12106638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!