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??