dexx1220
dexx1220

Reputation: 101

How to change server-side data in Django after a javascript/jquery event?

I'm new to Django and a fairly new programmer. I'm working on a battleship app on Django. I already have the 10X10 game board on my html pages, and using javascript/jquery these boards' contents change according to user interaction. However, how do I change the server-side data after a user-generated event. For example, a user clicks on a cell of the board and the cell content changes. But I want server-side data of the board to change to reflect the new contents of the cell. How does one go about doing this? Is it possible to execute Python code inside your javascript file?

Upvotes: 0

Views: 535

Answers (2)

AI Generated Response
AI Generated Response

Reputation: 8835

Using jQuery, as stated by dspeyer, you could use $.post to send the message. In general, a battleship app should work like this:

client - > server (information)

server - > client (response)

server - > opponent (sends info to opponent) (this is where you may have issues using purely ajax solutions - you'll need long polling/other server-push method)

and maybe a long-polling/comet connection so the server can push data to the client. There is the jQuery Comet plugin that implements the bayeux protocol.

You can execute Python code in your browser, using something like Skulpt, but I doubt that it is what you are looking for. I believe what you want is a server-side script executing in response to a client-side event, and for that your client would need to notify the server of the event, (POSTing data to a url, etc.) and your server would then be able to run the script.

Upvotes: 1

dspeyer
dspeyer

Reputation: 3036

This is what the $.post method is for. Then on the server side, have a post handler for that url that receives the data.

Upvotes: 1

Related Questions