gpt4 book ai didi

python - 如何在 Flask 中提交表单时检索范围内的 ORM 对象

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:53 25 4
gpt4 key购买 nike

我正在 flask 中开发一个博客应用程序。在显示帖子的 html 文件中,有评论选项。但我无法在提交表单时检索与评论表单对应的 ORM 对象“post”。我的index.html 文件:

{% extends "base.html" %}
{% block content %}


<h1>Hi, {{ g.user.nickname }}!</h1>
<form action="" method="post" name="post">
{{ form.hidden_tag() }}
<table>
<tr>
<td>Say something:</td>
</tr>
<tr>
<td>Title</td>
<td>{{ form.title(size=30, maxlength=140) }}</td>
<td>
{% for error in form.title.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %}
</td>
</tr>

<tr>
<td>Content</td>
<td>{{ form.post(size=30, maxlength=2000) }}</td>
<td>
{% for error in form.post.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %}
</td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="btn" value="Post!"></td>
<td></td>
</tr>
</table>
</form>


{% for post in posts.items %}
<div class="{{ post.id }}">

<div>
{% include 'post.html' %}
</div>
<hr>
<div class="row-fluid">
<div class="span11 offset1">
<div class="comments">
{% if post.comments %}
<p>
{% for comment in post.comments %}
<p><i>{{ comment.name }}</i>: {{ comment.body }}</p>
{% endfor %}
</p>
{% endif %}
</div>
</div>
</div>

<div>
<form action="" method="post" name="post">
{{ form.hidden_tag() }}
<input type="hidden" name="p_id" value="{{post}}">
<table>
<tr>
<td>Add a comment:</td>
</tr>
<tr>
<td>Name</td>
<td>{{ form.title(size=30, maxlength=40) }}</td>
<td>
{% for error in form.title.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %}
</td>
</tr>

<tr>
<td>Comment</td>
<td>{{ form.post(size=30, maxlength=140) }}</td>
<td>
{% for error in form.post.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %}
</td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="btn" value="Comment"></td>
<td></td>
</tr>
</table>
</form>
</div>

</div>
{% endfor %}


{% if posts.has_prev %}<a href="{{ url_for('index', page=posts.prev_num) }}">&lt;&lt; Newer posts</a>{% else %}&lt;&lt; Newer posts{% endif %} |
{% if posts.has_next %}<a href="{{ url_for('index', page=posts.next_num) }}">Older posts &gt;&gt;</a>{% else %}Older posts &gt;&gt;{% endif %}

{% endblock %}

查看文件有这个:

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST'])
@login_required
def index(page=1):
form = PostnCommentForm()
if form.validate_on_submit():
if request.form['btn'] == 'Post!':
post = Post(title=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), author=g.user)
db.session.add(post)
db.session.commit()
flash('Your post is now live!')
return redirect(url_for('index'))
else:
comment = Comment(name=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), parent_post=form.p_id.data)
db.session.add(comment)
db.session.commit()
flash('Your comment is now live!')
return redirect(url_for('index'))
posts = g.user.blog_posts().paginate(page, POSTS_PER_PAGE, False)
return render_template('index.html',
title='Home',
form=form,
posts=posts)

型号:

class Post(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(140))
body = db.Column(db.String(2000))
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comments = db.relationship('Comment', backref='parent_post', lazy='dynamic')

def __repr__(self):
return '<Post %r>' % (self.body)

class Comment(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(40))
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))

Form.py文件:

from flask.ext.wtf import Form
from wtforms import StringField, BooleanField, TextAreaField, IntegerField
from wtforms.validators import DataRequired, Length
from app.models import User

class PostnCommentForm(Form):
post = StringField('post', validators=[DataRequired()])
title = StringField('title', validators=[DataRequired()])
p_id= IntegerField('p_id')

最佳答案

如果您已经将 post_id 作为隐藏字段,那么检索它时会出现什么问题。

您可以检索它

comment = Comment(name=form.title.data, body=form.post.data, timestamp=datetime.utcnow(),title = form.post_id.data )

我想建议post_id = form.post_id.data改为title = form.post_id.data

关于python - 如何在 Flask 中提交表单时检索范围内的 ORM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com