【问题标题】:WTForm does not entirely repopulate form data upon editing a modelWTForm 在编辑模型时不会完全重新填充表单数据
【发布时间】:2015-06-06 00:03:15
【问题描述】:

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

我最近创建了一个“编辑帖子”表单,用户在该表单中单击一个链接,并将一些参数发送到一个函数。我的函数搜索帖子,并且 wtform 模块尝试使用来自目标帖子的数据重新填充表单。似乎 url (URLField)、category (SelectField) 和 name (StringField) 能够重新填充,但 details (TextField) 字段显示值无。任何人都可以在重新填充烧瓶wtform方面引导我走向正确的方向吗?

这是我的尝试:

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)

models.py

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',)

forms.py

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 %}

注意:CATEGORIES 是一个(长)字符串数组。

【问题讨论】:

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

标签: python flask wtforms peewee


【解决方案1】:

我已通过简单地将详细信息 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多