Python Forum
Django - Passing data from view for use client side with JavaScript in a template - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Django - Passing data from view for use client side with JavaScript in a template (/thread-10865.html)



Django - Passing data from view for use client side with JavaScript in a template - bountyhuntr - Jun-11-2018

Hello everyone,


I am developing a text-based RPG game in Django for learning purposes and I am wondering what the best way would be to run an event (such as a fight between two characters) server side, and instead of simply passing the result back to the client via a template, such as who won, I want to re-run this 'fight' but in a slower speed using JavaScript. This would show each characters 'turn' and the damage they dealt, although this would be processed instantly on the server.

The methods I thought about using were as follows:
  • Pass the combat log/fight through to the template via a context variable using some dictionary or JSON and access the template variable via JavaScript (if this is possible to even do, is it best avoided?)
  • Send this data to an API which the JavaScript code in the template will fetch and modify to show at a reduced speed

Does anyone have any better solutions? I don't think I should be posting the outcome of each fight using an API just to transfer this data to client side for use with Javascript, it's a waste of storage if this is the only use. I am wanted to do the first option - accessing it via a template variable using JavaScript which would be a dictionary

Thanks!