right now I am trying to add replies to my replies without adding a new table and route. does anyone know of any useful documentation that addresses similar problems?.
models.py
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) replys = db.relationship('Reply', backref='post') class Reply(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
routes.py
@app.route("/post/new", methods=['GET', 'POST']) @login_required def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('New Post Created', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post') @app.route("/post/<int:post_id>") def post(post_id): reply = Reply.query.filter_by(post_id=post_id).all() post = Post.query.get_or_404(post_id) return render_template('post.html', title=post.title, post=post, reply=reply) @app.route("/post/<int:post_id>/reply", methods=['GET', 'POST']) @login_required def new_reply(post_id): form = ReplyForm() post = Post.query.get_or_404(post_id) if form.validate_on_submit(): reply = Reply(title=form.title.data, content=form.content.data, author=current_user, post_id=post_id) db.session.add(reply) db.session.commit() flash('reply posted', 'success') return redirect(url_for('post', post_id=post.id)) return render_template('Reply.html', title='New Reply', form=form, legend='New Reply')
The reply route is where I'm trying to add a new function for replying to replies
https://stackoverflow.com/questions/65416244/how-to-thread-comments December 23, 2020 at 05:48AM
没有评论:
发表评论