简体   繁体   中英

<how to pass data from HTML/Javascript to be used in Django in Python and back

I'll Summarize the problem: = i'm a beginner at django, html, python, but i must find a way to do something because of my schools demo project.

To give absolutely minimum of the problem: I have a variable in html/javascript which i want to get and process in Django/python.

When i have processed info on django/python side, i want to be able to access it from a html/javascript pages.

I have tried to look into different things like Ajax, jquery, but i just don't understand it and most tutorials and such get way too wide and complex to grab that one thing i need. Thanks for any help :)

Yes , i have gone thorough stackoverflow similiar questions.

The simple answer is to use a basic HTML form and POST the data to your backend as a standard Web submission. If your value is in a javascript variable you can use javascript to write it to a hidden form field. Then you store the data somewhere on the backend(it can just be in memory at first) and use it in subsequent requests. Alternatively, you can do the entire thing in javascript using something like jQuery ajax to keep the api simple.

To make this work you need to have an endpoint implemented in your Django backend to handle the submitted data. Good documentation looks like https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms

There are multiple ways to do it first you should know web structure, web has two sides

1 - front end (static files html,css,js no server intervention )

2 - Back end (you can programme it with different languages like php or use a framework like django)

Back end : contain two sides also server side and Database side in Django its uses ORM which is Object-relational mapping that's mean it a way to connect server-side with SQl database

Now your question will be how to connect front-end with back-end

one way is to use API application programming interface in this case you will build an web application in server waiting for certain requests (those requests are made from your front-end like fetch requests) and Give it a response (response handled with your front-end the question here is when you get the data from the server how you will show it in the UI ? )

the other way is that Django follows the model-template-views (MTV) here is a good resource explain that

https://data-flair.training/blogs/django-architecture/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM