# Insert JavaScript variable into Django model

**URL:** <https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911>\
**Category:** Using Django\
**Created:** [December 31, 2020, 2:20pm UTC](https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911 "2020-12-31T14:20:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninspring](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ninspring/32/2970_2.png) [@ninspring](https://forum.djangoproject.com/u/ninspring)\
**Post date:** [December 31, 2020, 2:20pm UTC](https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911/1 "2020-12-31T14:20:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 31, 2020, 2:24pm UTC](https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911/2 "2020-12-31T14:24:33Z")

</div>

Always remember that the JavaScript is running in the browser, while Django is running on the server.

What you would need to do then is `POST` the data from the browser back to the server - generally either by submitting a form or submitting it as JSON through an AJAX call.

---

<div class="post-metadata">

**Author:** ![ninspring](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ninspring/32/2970_2.png) [@ninspring](https://forum.djangoproject.com/u/ninspring)\
**Post date:** [December 31, 2020, 3:17pm UTC](https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911/3 "2020-12-31T15:17:18Z")

</div>

Yes, of course.  
If I wanted to put simple input from HTML form, I’d use [this instructions](http://www.learningaboutelectronics.com/Articles/How-to-insert-data-into-a-database-from-an-HTML-form-in-Django.php).

However, I’m not sure how to POST a JS variable that is calculated basing on user input from html form.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 31, 2020, 3:29pm UTC](https://forum.djangoproject.com/t/insert-javascript-variable-into-django-model/5911/4 "2020-12-31T15:29:54Z")

</div>

Your choice:

- Use the variable to set the value of the form field before submitting the form.
- Create a JSON object to be submitted through an AJAX call
- Call a view passing that parameter as a URL parameter

Just remember though, you have _no_ control over the data being submitted. The user always has the ability at any time to modify what gets submitted, so sanitize it carefully.
