1

I'm trying to get the course.id which the user has clicked, but I don't know how to do so. Here are the codes: .py
elif request.method == "POST": # Adds course to favorite courses_dict = db.execute("SELECT * FROM courses;") for course in courses_dict: if request.form.get('name') == course['id']: db.execute("INSERT INTO favorite_courses(user_id, course_id) VALUES (?, ?);", session['user_id'], course['id']) return redirect("/")

.html ` {% for course in courses %}

    <hr class="border border-primary">
    <form action="/search" method="post">
        <button type="submit" name ='{{course.id}}'> Favorite
        </button>
    </form>
    <h2>{{ course.name }}</h2>

{% endfor %} {% endblock %} `

I'm trying to recive the 'course.id' which the user cliks.

  • Try looking at [How recieve name and value attribute of button in Flask](https://stackoverflow.com/questions/55515628/how-receive-name-and-value-attribute-of-button-in-flask). – Alias Cartellano Jun 28 '23 at 21:21
  • Welcome to stackoverflow. Please format blocks of code in your questions between three backticks for readability. See https://stackoverflow.com/editing-help – Rob Jun 28 '23 at 23:39

1 Answers1

0

The easiest way is probably to add a hidden input to your form and assign the value of the course ID to the value of the input.

<form action="/search" method="post">
  <input type="hidden" name="name" value="{{ course.id }}">
  <button type="submit">Favorite</button>
</form>
Rob
  • 518
  • 1
  • 3
  • 18