Hi,
What I would like to do, is insert the value of a JavaScript variable into Django model. Any ideas, how could this be achieved?
I have a model with few fields, like:
class Cat(models.Model):
LOW = 0
NORMAL = 1
HIGH = 2
VERY_HIGH = 3
LEVEL_CHOICES = (
(LOW, ‘Low’),
(NORMAL, ‘Medium’),
(HIGH, ‘High’),
(VERY_HIGH, ‘Very high’),
)
friendliness_level = models.IntegerField(choices=LEVEL_CHOICES, null=True)
attentiveness_level = models.IntegerField(choices=LEVEL_CHOICES, default=LOW)
sickness_level = models.IntegerField(choices=LEVEL_CHOICES, default=LOW)
Now, I’ve got a html form in which the user answers questions, each worth some amount of points. A script in JS sums those points in a variable. In the end, we have for example:
var friendliness = 30
What I would like to do now, is pass the value of this variable to django model as conditional statement:
if friendliness is lower than 5:
django Cat.friendliness_level = LOW
else if friendliness is lower than 20:
django Cat.friendliness_level = MEDIUM
else:
django Cat.friendliness_level = HIGH
My question is, how do I pass the value from JS to Django model?