Search code examples
djangodjango-viewshtmx

How to access the parameter given by hx-include in django views


I am trying to access a parameter in a createview passed by the hx-include function, but I can't seem to find the solution anywhere, and when I do, it doesn't work.

html:

<button
  id="deleteService"
  hx-post="{% url 'planner:create' %}"
  hx-include="[name='id']"
  type="button"
  class="btn btn-light btn-outline-black btn-sm"
  name="delete"
>Elimina</button>

and

<button
  hx-include="[name='{{ venue }}']"
  type="submit"
  class="btn btn-primary btn-lg">
  Submit
</button>

I tried

self.request.POST['name']
self.request.POST['id']
self.request.POST.get['name']
self.request.POST.get('name')

The errors are:

TypeError: 'method' object is not subscriptable

and without .get:

django.utils.datastructures.MultiValueDictKeyError: 'name


Solution

  • The request.POST is a QueryDict object which is a dictionary-like class customized to deal with multiple values for the same key. So you can use the .get() getter method on it. You need to provide the name of the included form input as the first parameter. In this case it's id:

    self.request.POST.get('id')