def edit_recipe(rid):
# Get the recipe
# c.execute('INSERT INTO recipes (title, location, ingredients, recipe, user) VALUES ("%s", "%s", "%s", "%s", "%s");' %
c, conn = connection()
_ = c.execute('SELECT * FROM recipes WHERE rid="%s"' % rid)
recipe = c.fetchone()
c.close()
conn.close()
gc.collect()
# Fill the form
form = RecipeForm(request.form)
form.title.data = recipe[1]
form.country.data = recipe[2]
form.ingredients.data = '\n'.join(recipe[3].split(','))
form.recipe.data = recipe[4]
if request.method == 'POST':
title = escape_string(request.form['title'])
country = escape_string(request.form['country'])
ingredients = escape_string(','.join(request.form['ingredients'].split('\r\n')).strip(','))
recipe = escape_string(request.form['recipe'])
# Update the DB
c, conn = connection()
c.execute('UPDATE recipes SET title="%s", location="%s", ingredients="%s", recipe="%s" WHERE rid=%s' % (title, country, ingredients, recipe, rid))
conn.commit()
# Close connection
c.close()
conn.close()
gc.collect()
flash('Recipe updated')
return redirect(url_for('user_page'))
return render_template('edit_recipe.html', form=form)
评论列表
文章目录