Flask + WTForms:更新一行

时间:2013-03-23 15:58:35

标签: sqlalchemy flask wtforms

我尝试更新数据库中的现有行。当我提交表单时,我被引导到/ item / edit /触发404.我在不同的项目中有类似的代码,并且工作正常。我无法弄清楚这有什么问题。

@main.route('/item/edit/<string:url>', methods = ['GET', 'POST'])
@login_required
def edit_item(url):
  i = get_item_by_url(url)
  form = ItemForm(obj=i)
  if form.validate_on_submit():
    i.title = form.title.data
    i.url = form.url.data
    i.body = form.body.data
    i.email = form.email.data
    i.private = form.private.data
    file = request.files['itempic']
    if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
      i.itempic = filename
    db.session.add(i)
    db.session.commit()
    flash('The Item: %s, has been updated succesfully.' % form.url.data)
    return redirect(url_for("index"))
  return render_template('/item/edit.html',
    form = form,
    i = i)

1 个答案:

答案 0 :(得分:1)

问题是您的方法已映射到/item/edit/something-here,而something-here将映射到url变量。

当您仅请求/item/edit/时,它不会映射到任何内容,因此会引发404。