首页 > 解决方案 > 我怎样才能用烧瓶和 Jinja2 做帖子的提要?

问题描述

我正在使用 flask-SQLAchemy 来管理我的数据库,并且我有一个名为post的表,其中包含内容、标题、日期、喜欢和 profile_id列,在提要中我使用了一个简单的 jinja2 模板,并且我发送了一个包含所有帖子的变量db,当我渲染帖子时,我会创建一个 for 循环来渲染它,但我无法获取发布帖子的人的姓名(我有一个带有他 id 的变量);所以我的问题是如何获取在 jinja 模板中发布帖子的用户的姓名?

标签: python-3.xflasksqlalchemyjinja2

解决方案


列“profile_id”是主键吗?如果是这样,那么您的帖子可能实际上显示的是帖子的 ID(即对应于主键或用于标识某个帖子的主列的数字)而不是您想要的。

class Post(db.Model):
    profile_id = db.Column(db.Integer, primary_key=True)
    likes = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(250))
    date_posted = db.Column(db.DateTime)
    content = db.Column(db.Text)

尝试分析上面的代码,我假设这是您的代码,您需要做的是添加另一列,在这种情况下,该列称为“作者”,因此请执行以下操作:

class Post(db.Model):
    profile_id = db.Column(db.Integer, primary_key=True)
    likes = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(250))
    author = db.Column(db.String(250))
    date_posted = db.Column(db.DateTime)
    content = db.Column(db.Text)

完毕?尝试通过执行以下操作打开终端并构建新模型:

python name-of-your-app.py

from name-of-your-app import *

db.create_all()

exit()

通过这样做,您可以确保实际使用的是您的新模型而不是旧模型。接下来要做的是将“作者”列添加到添加功能和编辑功能,然后确保在 jinja2 模板中为“作者”列添加新输入,并将“profile_id”替换为“作者” '。如果你还是一头雾水,请参考我在学习flask时使用的代码。另外,请参阅提供的链接,我也用它来制作提供的代码。

https://www.youtube.com/watch?v=XHGpPCYmPvI

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_simplelogin import SimpleLogin, is_logged_in
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
app.config['SECRET_KEY'] = 'something-secret'
app.config['SIMPLELOGIN_USERNAME'] = 'admin'
app.config['SIMPLELOGIN_PASSWORD'] = 'admin'
db = SQLAlchemy(app)
SimpleLogin(app)

class Blogpost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    subtitle = db.Column(db.String(50))
    author = db.Column(db.String(20))
    date_posted = db.Column(db.DateTime)
    content = db.Column(db.Text)

@app.route('/')
def index():
    posts = Blogpost.query.order_by(Blogpost.date_posted.desc()).all()

    return render_template('index.html', posts=posts)

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/add')
def add():
    if is_logged_in():
        return render_template('add.html')
    else:
        return redirect('/')

@app.route('/post/<int:post_id>')
def post(post_id):
    post = Blogpost.query.filter_by(id=post_id).one()

    return render_template('post.html', post=post)

@app.route('/edit/<int:post_id>')
def edit(post_id):
    post = Blogpost.query.filter_by(id=post_id).one()
    if is_logged_in():
        return render_template('edit.html', post=post)
    else:
        return redirect('/')

@app.route('/addpost', methods=['POST'])
def addpost():
    title = request.form['title']
    subtitle = request.form['subtitle']
    author = request.form['author']
    content = request.form['content']
    post = Blogpost(title=title, subtitle=subtitle, author=author, content=content, date_posted=datetime.now())

    db.session.add(post)
    db.session.commit()

    return redirect(url_for('index'))

@app.route('/deletepost/<int:post_id>', methods=['POST'])
def deletepost(post_id):
    post = Blogpost.query.get_or_404(post_id)

    db.session.delete(post)
    db.session.commit()
    return redirect(url_for('index'))

@app.route('/editpost/<int:post_id>', methods=['POST'])
def editpost(post_id):
    post = Blogpost.query.get_or_404(post_id)

    post.title = request.form['title']
    post.subtitle = request.form['subtitle']
    post.author = request.form['author']
    post.content = request.form['content']

    db.session.commit()
    return redirect('/')

if __name__ == "__main__":
    app.run(debug=True)

推荐阅读