I'm struggling to build an edit CRUD view in Flask. I have the add and list view working fine:
@main_blueprint.route('/', methods=['GET', 'POST'])
def all_items():
all_user_items = Items.query.filter_by()
return render_template('main/items.html', items=all_user_items)
@main_blueprint.route('/add', methods=['GET', 'POST'])
def add_item():
form = ItemsForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
try:
new_item = Items(form.name.data, form.notes.data)
db.session.add(new_item)
db.session.commit()
flash('Item added', 'success')
return redirect(url_for('main.all_items'))
except:
db.session.rollback()
flash('Something went wrong', 'danger')
return render_template('main/add.html', form=form)
But when designing the edit item view, it simply reroutes me back to the list view.
@main_blueprint.route('/edit_item/<items_id>', methods=['GET', 'POST'])
def edit_item(items_id):
form = EditItemsForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
try:
item = Items.query.get(items_id)
item.name = form.name.data
item.notes = form.notes.data
db.session.commit()
flash('Item edited successfully!', 'success')
return redirect(url_for('main.all_items'))
except:
db.session.rollback()
flash('Unable to edit item', 'danger')
return render_template('edit_item.html', item=item, form=form)
else:
flash('Something went wrong', 'danger')
return redirect(url_for('main.all_items'))
The log doesn't give any clues either even though I have full debug
127.0.0.1 - - [14/Aug/2022 10:32:29] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [14/Aug/2022 10:32:29] "GET /static/images/favicon.png HTTP/1.1" 404 -
Based on previous conversations, it sounds like it is failing within the TRY block and going straight to the last line. Code in context is here:
https://github.com/hiven/FlaskApp/blob/d9fce1b123f38aabbc48c63659a880a58b2fe44e/app/main/views.py
from Edit view fails within try block and goes straight to return with no error
No comments:
Post a Comment