0

I’m struggling to get flask helpful logs when I run into an issue. I have a view where the create, delete and list views all work, but when trying to do an update it simply reroutes me back to the list (/) page.

Even though I have enabled debug the only message in the logs I get is:

127.0.0.1 - - [13/Aug/2022 20:35:36] "GET /edit_item/5 HTTP/1.1" 302 -
127.0.0.1 - - [13/Aug/2022 20:35:36] "GET / HTTP/1.1" 200 -

This is my code. Specifically it is the edit route which as the issue, but wanted to show the rest of the code as that works 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)
    
@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"))


@main_blueprint.route('/delete_item/<items_id>')
def delete_item(items_id):
    item = Items.query.filter_by(id=items_id).first_or_404()
    db.session.delete(item)
    db.session.commit()
    flash('{} was deleted.'.format(item.name), 'success')
    return redirect(url_for('main.all_items'))

Where am I going wrong with this edit view, especially since seemingly it doesn't work but no error is shown even in development and debug mode.

Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • Did you try to debug the route? For me it seems like maybe something goes wrong in the `try` block and then the final return which redirects to the `main.all_items` is called. Or the validation of the Form is not successfull. Maybe add an `else` branch and flash a message or write one to the log – Kevin Aug 13 '22 at 20:13
  • Thank you for the reply. How should I debug the route? For example just trying to cut down the code and return a simple hello world and then add more code back in line by line, or something different. – Jimmy Aug 13 '22 at 20:15
  • do you use a Editor like Visual Studio Code? If so you can run step per step through the Application and look into each variable [Debug Flask](https://stackoverflow.com/questions/17309889/how-to-debug-a-flask-app) [Debug Flask in VSCode](https://code.visualstudio.com/docs/python/tutorial-flask#_run-the-app-in-the-debugger) – Kevin Aug 13 '22 at 20:20

0 Answers0