国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

python - ????+sqlalchemy? ???? ??? ??
怪我咯
怪我咯 2017-05-18 10:59:39
0
3
1054

??? ? ?????? ??? ?, ?????? ???? ???? ??? ? ???? ?? ???? ???? ??? ??? ?????. ????? ?? ??? ??? ????

怪我咯
怪我咯

走同樣的路,發(fā)現(xiàn)不同的人生

?? ??(3)
大家講道理

???????? ??? ???? ???? ???? ????. ??? ???? ?????? ?? ????. ??? ?? ? ????

?? ???? ??? ? ??? ???? ??? ??????.

?? ????? ???????. ???? ???? SQLAlchemy?? ??? ???? ?? ??? ?? render_template ??? ??? ?? .html ???? jinja2? ???? ? ???? ???? ??????.

?? ??, ?? ??? ??????? ?? ?? ????? ????? ??? ?? ????

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

好吧你不懂,就像這樣:

from . import db

class 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 Flask
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_pagedown import PageDown
from config import config

bootstrap = 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 SSLify
        sslify = 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

??? ???? ?????, ??? ????:

????

DB ????? ??? ??? ? ???? ?????? ? ???? __init__.py?? ?????! ??? ????? ?? ???? ?? ?? init ???? ????????

????

??? ??????? ???? ?? ?? python Manager.py ?? ???? ??????? ???????? ??? ?? ?????(??? ??? ?? ?????)
?? ??? ? ??? ?? ???? ??? ???????. ?? ???? ???? ???(?, views.py)? ???????. ?? ???? ???? index.html? ???? jinja2? ??? ????? ???????. ??? index.html? ??? ???? ???? ?? ?? Post? ???? ?? ??? ??? ???? ??? _post.html??? ???. index.html?? ????? {% include '_posts.html' %? ??? ???. }?????.
_post.html? ????????? ???? ???? "Flask ? ??: Python? ???? ? ???? ? ?????? ??"? ?? ?????. ??? ?? ??? ?? ??? ?? ??? ??? ???. ?? ????? ??? ? ?? ?? ??????. ????? ??? ?? ???? ? ?? ? ????? ??????! ??
洪濤

?? ?? /index ???? ???? ???? ?????
? ?? ?? ?? ??? ?? ? ?? ?? ??? ???? ???? ??? ??? ? ????

???
為情所困

????? ?????, ??? ??? ?????, request.args.get ??? ???? ????? ??? ? ????. ?? ?? ????? ???? ??? ????. ?? ?? ??? ??? ?? ???? ???? ?? return? ???? ????. ?? ? ???? ??? render_template? ???? jinjia2? ???? ??? ??? ????? ????.

Flask? ?? ??? ???????. http://docs.jinkan.org/docs/f...