How do you pass a variable from javascript to python (oTree) or django?
Using live pages seems to solve the issue.
https://otree.readthedocs.io/en/latest/live.html
So in Javascript:
function sendTracker1() {
liveSend({'which_char': 'char_1', 'value': tracker1})
}
And in Python:
class MyPage(Page):
form_model = 'player'
@staticmethod
def live_method(player, data):
if data['which_char'] == 'char_1':
player.timer_char1 = int(data['value'])
pass queryset data to javascript as a variable template
You can use Django builtin serilizers
to pass data to js for queryset.
# views.py
from django.core import serializers
historicals = serializers.serialize("json", Deliveries.objects.all())
# html
<script type="text/javascript">
// construct js objects
var actuals = JSON.parse('{{ historicals | safe }}')
</script>
EDIT
if you would like to loop in django template, you just need to pass delivers
queryset, no need parse as js variables.
# views.py
diliveries = Deliveries.objects.all()
# html
{% for i in deliveries %}
{{ i.Date }}
{% endfor %}
Related Topics
React Native How to Call Multiple Functions When Onpress Is Clicked
How to Sort Elements in an Array into Order and Even Numbers and They Should Be in Ascending Order
Javascript Regex: Replace Hyphen Between Characters Only (Not Spaces)
Jquery Class on Click Works on Desktop But Not Mobile
Combine Multiple JavaScript Files into One Js File
Vuejs Set Active Class, When One Li Element Clicked in V-For Loop
Uploading Multiple Files Using Formdata()
React Js - Uncaught Typeerror: This.Props.Data.Map Is Not a Function
Referenceerror: Cannot Access 'Player' Before Initialization
How to Get Parent Width/Height in React Using Hooks
Useselector Not Updating When Store Has Changed in Reducer. Reactjs Redux
Disable Right-Clicking in Html5 Video
Delete Span Tag Along With Text in HTML Permanently
How to Highlight Parts of an Imagemap on Mouseover
Disable Click Outside of Bootstrap Modal Area to Close Modal
How to Get Values of Checkbox in Node Js Using Ejs
How to Combine Object Values from Separate Lines into One Line