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

时间:2015-06-06 00:03:15

标签: python flask wtforms peewee

  

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

我最近创建了一个“编辑帖子”表单,用户在其中单击一个链接,并将一些参数发送到一个函数。我的函数搜索帖子,wtform模块尝试使用来自目标帖子的数据重新填充表单。似乎 url (URLField),类别(SelectField)和名称(StringField)能够重新填充,但详细信息(TextField)字段显示值None。任何人都可以指导我在重新填充烧瓶重量方面朝正确的方向发展吗?

这是我的尝试:

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是一个(长)字符串数组。

1 个答案:

答案 0 :(得分:0)

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