1

I am attempting to create a button that allows for a user to delete a user on the my system by passing through a post request to the following view. My challenge is that I cannot get submission portion of the form to trigger a POST request, it always generates GET and then fails to delete the user :

<a href="{{ url_for('users.user_delete_admin', id=user.id) }}" class="btn btn-danger btn-xs" >Delete</a>

I have tried passing in method ="post" as a parameter to the url_for method, but that doesn't seem to work

My view handler

@users_blueprint.route('/admin/delete-user/<int:id>', methods=["GET","POST"])
@login_required
@admin_login_required
def user_delete_admin(id):
    user = User.query.get((id))
    if request.method =="POST":
       user.delete()
       db.session.commit()
       return redirect(url_for('users.users_list_admin'))
    users = User.query.all()
    return render_template('users-list-admin.html', users=users, current_user=current_user)

And the html form:

    <table class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Is Admin ?</th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      {% for user in users %}
        <tr>
          <td>{{ user.id }}</td>
          <td>{{ user.name }}</td>
          <td>{{ user.admin }}</td>
          <td>
            <a href="{{ url_for('users.user_update_admin', id=user.id) }}" class="btn btn-info btn-xs">Edit</a>
          </td>
          <td>
            <a href="{{ url_for('users.user_delete_admin', id=user.id) }}" class="btn btn-danger btn-xs" >Delete</a>
          </td>
        </tr>
      {% endfor %}
    </tbody>
  </table>

Handling this with a simple form is relatively simple, but for some reason I cannot get this to function properly.

Thanks

davidism
  • 121,510
  • 29
  • 395
  • 339
Geri Atric
  • 415
  • 1
  • 4
  • 16

1 Answers1

1

Handled it like this:

<form  method = "post" action = "{{ url_for('users.user_delete_admin', id=user.id) }}">
            <button class="btn btn-danger btn-xs" type="submit">Delete</button>
            </form>
Geri Atric
  • 415
  • 1
  • 4
  • 16