1

So this is my template where I have a table with products. Each product has a default "in_cart" value of False. So each row has a submit button "add to cart". I would like to change on click the boolean value to true so it appears in /cart. this is the table:

{% load staticfiles %}

  <table>
    <tr>
        <th>List of car parts available:</th>
    </tr>
    <tr>
        <th>Name</th>
        <th>Price</th>
    </tr>
    {% for product in products_list %}
    <tr>
      <td>{{ product.name }}</td>
      <td>${{ product.price }}</td>
      <td>{% if not product.in_cart %}
              <form action="{% url 'cart' %}" method="post">
                {% csrf_token %}
                <input type="submit" id="buttonId" value="Add to cart">
              </form>
          {% else %}
              {{ print }}
          {% endif %}
      </td>
    </tr>
    {% endfor %}
  </table>

I created a file in myapp/static/submit.js

$('#buttonId').click(function() {
    $.ajax({
        url: 'cart/',
        method: 'POST',
        data: {
            name: product.in_cart, 
            click: true
        }
        success: function (data) {
        }
    });
});

Don't really know how to go from there.

NicoBar
  • 555
  • 1
  • 7
  • 16
  • No code will be executed return statement. Just keep that in mind – Dmitry Arkhipenko Jun 23 '18 at 13:08
  • Please don't repeat questions. Simply edit your original question with any new information you have or any new code you'd tried. It will bump your question to the top of the active queue. – rafalmp Jun 23 '18 at 13:34
  • Using AJAX can solve this problem. Use jQuery to make things easier. – Dhaval Savalia Jun 23 '18 at 18:45
  • Thank you. I have never used javascript before, how can I integrate it with my Django app? Should I create a separate file and then reference it in my HTML? – NicoBar Jun 23 '18 at 22:57

0 Answers0