2013-03-23 62 views
0

我嘗試更新數據庫中的現有行。當我提交表單時,我將指向/ item/edit /這會觸發404。我在不同的項目中使用類似的代碼,並且這種代碼正常工作。我無法弄清楚這有什麼問題。Flask + WTForms:更新一行

@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

的問題是你的方法被映射到/item/edit/something-heresomething-here將被映射到url變量。

當你請求只是/item/edit/它不映射到任何東西,因此提出了一個404

+0

我應該在我的代碼更改,因此正確映射? – cancerballs 2013-03-23 17:09:21

+2

你的表單提交應該像'{{url_for('edit_item',url =「whatever」)}}' – Doobeh 2013-03-24 00:12:31