[Django]-How to pass a parameter to a view with HTMX and Django?

6👍

You’ve sent your id in url query params as ?id=1 not in request body. Query params can be accessed from request.GET

item_id = request.GET.get('id')

If you want to send id in request body add a hidden input field, and add hx-include="[name='id']" to include that field

<td id="like-{{ item.id }}"
            hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
            hx-post="{% url 'save-like' %}"
            hx-target="#like-{{ item.id }}" 
            hx-include="[name='id']"
            hx-swap="outerHTML">

  <input type="hidden" value="{{item.id}}" name="id">
  {{ item.like }}
</td>

Then you can access from request.POST['id']

Leave a comment