user3799942
user3799942

Reputation: 289

Django forms and javascript or maybe some other way to do the same

Im having a problem with this. I have a ModelForm call FacturaForm that is based on this on this model:

class Factura(models.Model):
    TIPO_FACTURA = (
        ('A', 'A'),
        ('E', 'E')
        )
    RESPONSABLE = (
        ('Responsable Inscripto', 'Responsable Inscripto'),
        ('Responsable No Inscripto', 'Responsable No Inscripto')
        )
    tipo_Factura = models.CharField(max_length=1, choices= TIPO_FACTURA)
    nombre_cliente = models.ForeignKey(Cliente)
    fecha_factura = models.DateField()
    RI = models.CharField(max_length=50, choices= RESPONSABLE)
    numero_De_Factura = models.IntegerField(max_length=50)
    descripcion = models.CharField(max_length=140)
    importe_Total= models.FloatField()
    iva = models.FloatField()
    importe_sin_iva = models.FloatField()

So now i have the form with its fields so the user can complete them. But i have a problem. There is a form field call "importe_sin_iva", this is were the user is going to enter a number, but there is also the field call "iva", this field should calculate a number doing ("importe_sin_iva" * 0.21 ) and what the user WANTS is to write a number on the "importe_sin_iva" field and that the sistem calculates automatically the result of the multiplication and fill the box with it. And there is also the "importe_Total" field that has to do something similar but this time it has to add (+) the "importe_sin_iva" + the "iva" field and fill this box with the result

My question is this: Is there a proper way to do this on a django form? or can it be done with javascript? and in case it must be done in javascript, can anyone give me a hint on how to do such thing because i have never touched javascript.

If you think i shoul change the model let me know.

If you have any other solution to do this it will be appreciated.

Thank you.

Upvotes: 0

Views: 26

Answers (1)

cezar
cezar

Reputation: 12032

If you want to show the values to the user as he is filling in the form, you'll need JavaScript. At least I don't know any other client side technique to do this. Your real big problem here is to persist the data to database. You can't rely to the client that the proper values are going to be saved in the database. For this purpose you could overwrite the save method of the model. This is a simple example:

def save(self, *args, **kwargs):
    self.iva = self.importe_sin_iva * 0.21
    self.importe_Total = self.iva + self.importe_sin_iva
    super(Factura, self).save(*args, **kwargs)

In the form you can use the event 'onchange' to update the appropriate fields on the fly. The framework jQuery is ideal for this. Your function could look like this:

$('#importe_sin_iva').change(function() {
    var importe_sin_iva = $(#importe_sin_iva).val()
    var iva = importe_sin_iva * 0.21
    $('#iva').val(iva)
    var total = importe_sin_iva + iva
    $('#total').val(total)
});

This is also a simple example, where the input fields for importe_sin_iva, iva and importe_Total have the IDs importe_sin_iva, iva and total respectively. The fields iva and total shouldn't be persisted to the database. You can't rely on client side techniques for validation of the values.

You still have to learn how to use jQuery, how to bind it to your HTML, etc. I hope these simple examples can point you the way.

Upvotes: 1

Related Questions