Hi I installed the tax code calculation app.
I inserted the function in the POST function and when it saves it performs the calculation and stores the Tax Code.
I would like to make it work like this, to the right of the field insert a button and on its click perform the calculation with its result display.
I think it should be done with js but I didn’t understand how to call the function from JS
Or is there any other method ?
I post the code of the view
class AssociatiInseView(View):
template_name = 'Pale/Tabelle/AssociatiInse.html'
def get(self, request):
form = AssociatiForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = AssociatiForm(request.POST)
if form.is_valid():
associato = form.save(commit=False)
# # comune.Anagrafica = request.Anagrafica
# cognome = form.cleaned_data.get('Cognome', '')
# nome = form.cleaned_data.get('Nome', '')
# associato.Anagrafica = cognome + ' ' + nome
# Calcolo del C.F.
lastname = form.cleaned_data.get('Cognome')
firstname = form.cleaned_data.get('Nome')
gender = form.cleaned_data.get('Sesso')
birthdate = form.cleaned_data.get('DataNascita')
# Conversione della Data in Stringa
birthdate_str = birthdate.strftime('%d/%m/%Y')
# birthdate_str = form.cleaned_data.get('DataNascita')[0].strftime('%d/%m/%Y') if isinstance(
# form.cleaned_data.get('DataNascita'), tuple) else form.cleaned_data.get('DataNascita').strftime(
# '%d/%m/%Y')
birthplace = form.cleaned_data.get('ComuneNascitaID').Comune
print(' birth ', birthdate, birthdate_str)
associato.CodFiscale = codicefiscale.encode(
lastname = lastname,
firstname = firstname,
gender = gender,
birthdate = birthdate_str,
birthplace = birthplace,
)
# print('c.f. ', associato.CodFiscale)
# Quando non viene inserita la foto, memorizza il link al file di Base
if 'Foto' in request.FILES:
associato.Foto = request.FILES['Foto']
else:
# Se nessuna nuova immagine è stata fornita, mantieni il valore predefinito
associato.Foto = 'Sito/Mamozio.png'
associato.save()
# Aggiungi un messaggio di successo
messages.success(request, f"ANAGRAFICA '{associato.Anagrafica}' - SALVATA ")
# Azzerare i campi del form
form = AssociatiForm()
else:
# Stampa tutti gli errori dei campi del form nel terminale
for field, errors in form.errors.items():
messages.error(request, f"Errore nel campo {field}: {', '.join(errors)}")
print(f"Errore nel campo {field}: {', '.join(errors)}")
print("Errore nel form.")
# return HttpResponseBadRequest("Errore nel form. .")
# Redirect alla stessa pagina
return redirect(reverse('AssociatiInse'))