我尝试更新数据库中的现有行。当我提交表单时,我被定向到触发 404 的 /item/edit/。我在不同的项目中有类似的代码并且可以正常工作。我不知道这有什么问题。
@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)