Django HTMX never clean a modal

hello i have a modal that work in this way:

<dialog id="contact_modal" class="modal">
<div class="modal-box">
<h3 class="font-bold text-lg mb-4">{{ testo }}</h3>
<form
{% if testo == "Aggiungi Cliente" %}
  hx-post="{% url 'aggiungi_cliente' %}" 
{% else %}
   hx-post="{% url 'aggiungi_cliente_esistente' %}" 
{% endif %}
hx-target='#contact-table-body'
hx-swap="afterbegin"
hx-on:success="contact_modal.close(); this.reset();"
class="mb-4">
{% csrf_token %}
<div class="form-control w-full">
  <label class="label">
    <span class="label-text">Ragione Sociale</span>
    {{ form.ragione}}
    {% if form.ragione.errors %}
      <label class="label text-error">
        {{form.ragione.errors}}
      </label>
    {% endif %}
</div>........
<div class="modal-action">
<button type="button" class="btn"
  onclick="contact_modal.close()">Cancella</button>
  {% if testo == "Aggiungi Cliente" %}
    <button type="submit" class="btn btn-primary">Aggiungi Cliente</button>
  {% else %}
    <button type="submit" class="btn btn-primary">Ignora e Aggiungi</button>
  {% endif %}

and then the views like this:

@require_http_methods(\[‘POST’\])
def aggiungi_cliente(request):
    form = ClientiForm(request.POST)
    if form.is_valid():
        ragione = form.cleaned_data\[‘ragione’\]
        cliente_esistente = Clienti.objects.filter(ragione=ragione)
    if cliente_esistente.count() > 0:
        testo = ‘Cliente Trovato’
        dati = form.save(commit=False)
        response = render(request, ‘polls/partial/add-contact-modal.html’, {‘form’: form, ‘testo’: testo,})
        response\[‘HX-Trigger’\] = ‘trovato’
        response\[‘HX-REtarget’\] = ‘#contact_modal’
        response\[‘HX-reswap’\] = ‘outerHTML’
        response\[‘HX-Trigger-After-Settle’\] = ‘fail’
        return response
    else:
        dati = form.save(commit=False)
        dati.save()
        response = render(request, ‘polls/partial/contact-row.html’, {‘dati’: dati,})
        response\[‘HX-Trigger’\] = ‘success’
        return response
    else:
        print(‘form non valida’)


@require_http_methods(\[‘POST’\])
def  aggiungi_cliente_esistente(request):
        form = ClientiForm(request.POST)
        if form.is_valid():
            dati = form.save(commit=False)
            dati.save()
            testo = ‘Aggiungi Cliente’
            response = render(request, ‘polls/partial/contact-row.html’, {‘dati’: dati, ‘testo’: testo,})
            response\[‘HX-Trigger’\] = ‘success’
            return response

If the contatto never exist all work ok, but if the contatto exists, the modal closed but the reset don’t work “hx-on:success=“contact_modal.close(); this.reset();” why???

So if i open the modal after the insertion of an exesting cliente , the modal is not reset and also the variable testo is not updated, basicalli he call me again the function aggiungi_cliente_esistente, but i need to call only aggiungi cliente

What is wrong??

Side Note: When posting code here, enclose the code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (I have taken the liberty of correcting your original post.
Please remember to do this in the future.)

The first thing I do when trying to solve an HTMX-related issue is to examine the console log of the browser. HTMX does a good job of logging errors when they occur. If there isn’t any useful information there, then I use the htmx.logAll() function to show me everything that is going on. That usually gives me enough information to narrow down the scope of the issue.