亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

python - 用flask+sqlalchemy查詢數(shù)據(jù)

瀏覽:80日期:2022-07-03 17:47:16

問題描述

再我登陸計入到主頁面后,主頁面的數(shù)據(jù)是用form表單接收的嗎?查到的數(shù)據(jù)不知道怎么放到頁面上,有沒有什么資料推薦一下,或者是給點指導(dǎo)

問題解答

回答1:

我想把從數(shù)據(jù)庫查到的數(shù)據(jù)顯示到頁面上,我想知道是不是用form提交上去的?找不到參考資料

我將根據(jù)這一句話來回答樓主的問題。

我先概括一下思路:用路由把SQLAlchemy查詢到的數(shù)據(jù)通過參數(shù)傳遞給render_template函數(shù),再在.html文件中用jinja2實現(xiàn)動態(tài)渲染網(wǎng)頁。

比如現(xiàn)在你有一個博客數(shù)據(jù)庫,你需要把博客的內(nèi)容顯示到主頁上,該如何顯示呢?

主要的方法其實是用jinja2來實現(xiàn),首先假設(shè)你有一個Post數(shù)據(jù)庫(已經(jīng)在models.py中定義好了的,別跟我說你不懂?。。?/p>

好吧你不懂,就像這樣:

from . import dbclass Post(db.Model): __tablename__ = ’posts’ id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) body_html = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) author_id = db.Column(db.Integer, db.ForeignKey(’users.id’)) comments = db.relationship(’Comment’, backref=’post’, lazy=’dynamic’) db.event.listen(Post.body, ’set’, Post.on_changed_body)

什么你不懂db是哪里import來的?是app包里__init__.py來的呀!這里懶得解釋了,直接帖個完整的init方法吧

from flask import Flaskfrom flask_bootstrap import Bootstrapfrom flask_mail import Mailfrom flask_moment import Momentfrom flask_sqlalchemy import SQLAlchemyfrom flask_login import LoginManagerfrom flask_pagedown import PageDownfrom config import configbootstrap = Bootstrap()mail = Mail()moment = Moment()db = SQLAlchemy()pagedown = PageDown()login_manager = LoginManager()login_manager.session_protection = ’strong’login_manager.login_view = ’auth.login’def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) bootstrap.init_app(app) mail.init_app(app) moment.init_app(app) db.init_app(app) login_manager.init_app(app) pagedown.init_app(app) if not app.debug and not app.testing and not app.config[’SSL_DISABLE’]:from flask_sslify import SSLifysslify = SSLify(app) from .main import main as main_blueprint app.register_blueprint(main_blueprint) from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix=’/auth’) from .api_1_0 import api as api_1_0_blueprint app.register_blueprint(api_1_0_blueprint, url_prefix=’/api/v1.0’) return app

不過更改數(shù)據(jù)庫記得先運行python manager.py shell來遷移一下數(shù)據(jù)庫呀(具體的自己查去)扯遠了,我們來看樓主的問題。

首先來看路由(就是views.py)中的內(nèi)容:

@main.route(’/’, methods=[’GET’, ’POST’])def index(): #前面已經(jīng)假設(shè)了你有個Post數(shù)據(jù)庫 query = Post.query #這里使用了pagination,就是自動實現(xiàn)翻頁的一個擴展,可用可不用哈 pagination = query.order_by(Post.timestamp.desc()).paginate(page, per_page=current_app.config[’FLASKY_POSTS_PER_PAGE’],error_out=False) #這里才是重點,簡單來說就是讓posts=Post.query.order_by(Post.timestamp.desc()) posts = pagination.items #然后用render_template傳給html,交給jinja2來動態(tài)渲染 return render_template(’index.html’, form=form, posts=posts, show_followed=show_followed, pagination=pagination)

現(xiàn)在讓我們到index.html中來看看jinja2該如何工作,不過為了讓index.html看上去盡量簡潔,我將打印Post的模塊單獨提了出來,叫_post.html,在index.html中使用只要{% include ’_posts.html’ %}即可:讓我們來看_post.html

<ul class='posts'> <!--處理從路由傳進來的posts,用一個for循環(huán)處理,語法酷似python--> {% for post in posts %} <li class='post'><p class='post-thumbnail'> <a href='http://www.aoyou183.cn/wenda/{{ url_for(’.user’, username=post.author.username) }}'><img src='http://www.aoyou183.cn/wenda/{{ post.author.gravatar(size=40) }}'> </a></p><p class='post-content'> <!--Post數(shù)據(jù)在這里顯示,你要的答案就在這了。核心思想就是用jinja2 --> <p class='post-date'>{{ moment(post.timestamp).fromNow() }}</p> <p class='post-author'><a href='http://www.aoyou183.cn/wenda/{{ url_for(’.user’, username=post.author.username) }}'>{{ post.author.username }}</a></p> <p class='post-body'>{% if post.body_html %} {{ post.body_html | safe }}{% else %} {{ post.body }}{% endif %} </p></p> </li> {% endfor %}</ul>

以前看過一點《Flask Web開發(fā):基于Python的Web應(yīng)用開發(fā)實戰(zhàn)》,今天小小復(fù)習(xí)一下,如果有什么不對的地方,請大家指出,謝謝!

樓主要的答案這本書里都有,也強烈推薦想學(xué)flask的同學(xué)看看這本書呀!

回答2:

比如你訪問的是/index頁面,你肯定會有一個后端,一個前端給你提供思路,你自己去找相關(guān)資料,搜索引擎搜索flask開發(fā)

@app.route(’/index’)def index(): data = '從數(shù)據(jù)庫讀取出來的數(shù)據(jù)' html = [] for item in data:html.append(item.'列名') return ’’.join(html)回答3:

你接受參數(shù),可以用路由里面的變量,也可以用request.args.get命令獲取參數(shù)。然后執(zhí)行程序獲得結(jié)果,當(dāng)然最簡單的就是拼接成字符串直接用return,當(dāng)然更正式的是用render_template ,配合jinjia2渲染模板輸出。

這個還是看下flask的快速入門吧。http://docs.jinkan.org/docs/f...

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲黄色在线 | 在线网站cosplay福利视频 | 国产黄色免费看 | 小明看看在线观看 | 日本aaaa级 | 国产精品精品国产一区二区 | 中文字幕乱码一区三区免费 | 亚洲青青 | 青青青在线日本免费视频 | 国产成人精品一区二区免费 | 善良的翁熄日本中文字幕1 上海麻豆文化传媒网站入口 | 免费的很黄很色的床小视频 | 高清视频黄色录像免费 | 天天拍久久| 毛片免费软件 | 狠狠色丁香九九婷婷综合五月 | 国产成人精品区在线观看 | 色婷婷狠狠久久综合五月 | 中文字幕一区日韩在线视频 | 不卡国产视频 | 欧美亚洲一区二区三区四 | 日韩国产欧美一区二区三区 | 久久爱综合网 | 国内真实下药迷j在线观看 国内主播大秀福利视频在线看 | 亚洲 中文 欧美 日韩 在线人 | 97视频免费在线观看 | 国产成人免费观看在线视频 | 色爱区综合激月婷婷激情五月 | 麻豆传媒2021精品传媒一区 | 成人www| 美女扒开胸罩露出奶了无遮挡免费 | 亚洲免费福利 | 亚洲b| 午夜看一级特黄a大片黑 | 免费黄色一级网站 | 欧美高清一级片 | 欧美一级一片 | 黄色高清视频 | 精品无人区乱码一区二区三区手机 | 轻轻操在线播放 | 国产大片免费看 |