Python WTForm不会在编辑模型时完全重新填充表单数据

Python WTForm不会在编辑模型时完全重新填充表单数据,python,flask,wtforms,peewee,Python,Flask,Wtforms,Peewee,编辑:类别字段现在可以重新填充。犯了一个错误 检索数据。App.py已被编辑 我最近创建了一个“编辑帖子”表单,在该表单中,用户单击一个链接,并向函数发送一些参数。My函数搜索帖子,wtform模块尝试使用来自目标帖子的数据重新填充表单。似乎url(url字段)、类别(SelectField)和名称(StringField)可以重新填充,但详细信息(TextField)字段显示的值为无。在重新填充表格方面,有人能指导我朝着正确的方向吗 以下是我的尝试: app.py @app.route('/e

编辑:类别字段现在可以重新填充。犯了一个错误 检索数据。App.py已被编辑

我最近创建了一个“编辑帖子”表单,在该表单中,用户单击一个链接,并向函数发送一些参数。My函数搜索帖子,wtform模块尝试使用来自目标帖子的数据重新填充表单。似乎url(url字段)、类别(SelectField)和名称(StringField)可以重新填充,但详细信息(TextField)字段显示的值为无。在重新填充表格方面,有人能指导我朝着正确的方向吗

以下是我的尝试:

app.py

@app.route('/edit_post/<int:post_id>', methods=("GET","POST"))
@login_required
def edit_my_post(post_id):
    posts = models.Post.select().where(models.Post.id == post_id)
    if posts.count() == 0:
        abort(404)


    if post_user.id == current_user.id :
        post = models.Post.select().where(models.Post.id == post_id).get()
        form = forms.ProductForm(obj=post)

        if form.validate_on_submit():
            form.populate_obj(post)
            query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.details.data.strip(),
                               url = form.url.data,
                               category = = form.category.data
                              ).where(models.Post.id == post_id)
            query.execute()
            flash("Your post has been edited.", "success")
            return redirect(url_for('index'))
    else:
        flash("Ay! Stay away from that post!","warning")
        return redirect(url_for('index'))

    return render_template("edit.html",form=form)
class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        rel_model = User,
        related_name = 'posts'
    )
    name = TextField()
    content = TextField()
    upvotes = IntegerField(default=0)
    url = TextField()
    category = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)
class ProductForm(Form):
    name = StringField('Content Name', validators = [DataRequired()])
    url = URLField(validators=[url()])
    details = TextAreaField('Content Summary', validators = [DataRequired(),Length(max=140)])
    category = SelectField("Choose a category that your content fits in.", choices=CATEGORIES,coerce=int)
forms.py

@app.route('/edit_post/<int:post_id>', methods=("GET","POST"))
@login_required
def edit_my_post(post_id):
    posts = models.Post.select().where(models.Post.id == post_id)
    if posts.count() == 0:
        abort(404)


    if post_user.id == current_user.id :
        post = models.Post.select().where(models.Post.id == post_id).get()
        form = forms.ProductForm(obj=post)

        if form.validate_on_submit():
            form.populate_obj(post)
            query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.details.data.strip(),
                               url = form.url.data,
                               category = = form.category.data
                              ).where(models.Post.id == post_id)
            query.execute()
            flash("Your post has been edited.", "success")
            return redirect(url_for('index'))
    else:
        flash("Ay! Stay away from that post!","warning")
        return redirect(url_for('index'))

    return render_template("edit.html",form=form)
class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        rel_model = User,
        related_name = 'posts'
    )
    name = TextField()
    content = TextField()
    upvotes = IntegerField(default=0)
    url = TextField()
    category = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)
class ProductForm(Form):
    name = StringField('Content Name', validators = [DataRequired()])
    url = URLField(validators=[url()])
    details = TextAreaField('Content Summary', validators = [DataRequired(),Length(max=140)])
    category = SelectField("Choose a category that your content fits in.", choices=CATEGORIES,coerce=int)
edit.html

{% extends "layout.html" %}
{% from 'macros.html' import render_field %}

{% block content %}
<form method="POST" action="">
  {{ form.hidden_tag() }}
  {% for field in form %}
    {{ render_field(field) }}
  {% endfor %}
  <br>
  <button id="submit" type="submit">Edit Post</button>
</form>
{% endblock %}
{%extends“layout.html”%}
{%from'macros.html'导入呈现_字段%}
{%block content%}
{{form.hidden_tag()}}
{%形式的字段为%}
{{render_field(field)}
{%endfor%}

编辑文章 {%endblock%}
注意:CATEGORIES是字符串的(长)数组


我只需将details ProductForm字段重命名为与Post表单上的属性相同的名称,就解决了这个问题。现在表单代码如下所示:

class ProductForm(Form):
    name = StringField('Content Name', validators = [DataRequired()])
    url = URLField(validators=[url()])
    content = TextAreaField('Content Summary', validators = [DataRequired(),Length(max=140)])
    category = SelectField("Choose a category that your content fits in.", choices=CATEGORIES,coerce=int)
我重命名了部分查询以检索数据,如下所示:

query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.content.data.strip(),
                               url = form.url.data,
                               category = form.category.data
                              ).where(models.Post.id == post_id)
            query.execute()

你也应该发布你的模板表单代码。我也实现了模板代码。