简体   繁体   English

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

[英]<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.我将总结问题:=我是 django、html、python 的初学者,但由于我的学校演示项目,我必须找到一种方法来做某事。

To give absolutely minimum of the problem: I have a variable in html/javascript which i want to get and process in Django/python.给出绝对最小的问题:我在 html/javascript 中有一个变量,我想在 Django/python 中获取和处理它。

When i have processed info on django/python side, i want to be able to access it from a html/javascript pages.当我在 django/python 端处理信息时,我希望能够从 html/javascript 页面访问它。

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.我试图研究不同的东西,比如 Ajax、jquery,但我只是不理解它和大多数教程,这样的东西太宽泛和复杂,无法抓住我需要的一件事。 Thanks for any help :)谢谢你的帮助 :)

Yes , i have gone thorough stackoverflow similiar questions.是的,我已经彻底解决了 stackoverflow 类似的问题。

The simple answer is to use a basic HTML form and POST the data to your backend as a standard Web submission.简单的答案是使用基本的 HTML 表单并将数据作为标准 Web 提交发布到后端。 If your value is in a javascript variable you can use javascript to write it to a hidden form field.如果您的值在 javascript 变量中,您可以使用 javascript 将其写入隐藏的表单字段。 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.或者,您可以使用 jQuery ajax 之类的东西在 javascript 中完成整个事情,以保持 api 简单。

To make this work you need to have an endpoint implemented in your Django backend to handle the submitted data.要完成这项工作,您需要在 Django 后端实现一个端点来处理提交的数据。 Good documentation looks like https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms好的文档看起来像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 ) 1 - 前端(静态文件 html、css、js 无服务器干预)

2 - Back end (you can programme it with different languages like php or use a framework like django) 2 - 后端(您可以使用不同的语言(如 php)或使用框架(如 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后端:在 Django 中包含服务器端数据库端两个方面,它使用ORM ,这是对象关系映射,这意味着它是一种将服务器端与SQl数据库连接的方式

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 ? )一种方法是使用API应用程序编程接口,在这种情况下,您将在服务器中构建一个 Web 应用程序,等待某些请求(这些请求是从您的前端发出的,如 fetch 请求)并给它一个响应(由您的前端处理的响应)结束这里的问题是,当您从服务器获取数据时,您将如何在 UI 中显示它?)

the other way is that Django follows the model-template-views (MTV) here is a good resource explain that另一种方式是Django遵循模型模板视图(MTV)这里是一个很好的资源解释

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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