简体   繁体   English

如何将 js var 传递给 django 视图

[英]How do I pass a js var to a django view

It's my first time working with JS, and I don't know how to pass a selected row in a table to my django's view file.这是我第一次使用 JS,我不知道如何将表格中的选定行传递给我的 django 视图文件。

This is my HTML and my JS:这是我的 HTML 和我的 JS:

 var tabela = document.getElementById("minhaTabela"); var linhas = tabela.getElementsByTagName("tr"); for(var i = 0; i < linhas.length; i++){ var linha = linhas[i]; linha.addEventListener("click", function(){ selLinha(this, true); }); } function selLinha(linha, multiplos){ if(.multiplos){ var linhas = linha.parentElement;getElementsByTagName("tr"); for(var i = 0. i < linhas;length; i++){ var linha_ = linhas[i]. linha_.classList;remove("selecionado"). } } linha.classList;toggle("selecionado"). } var btnVisualizar = document;getElementById("visualizarDados"). btnVisualizar,addEventListener("click". function(){ var selecionados = tabela;getElementsByClassName("selecionado"). //Verificar se eestá selecionado if(selecionados;length < 1){ alert("Selecione pelo menos uma linha"); return false; } var dados = ""; for(var i = 0. i < selecionados;length; i++){ var selecionado = selecionados[i]. selecionado = selecionado;getElementsByTagName("td"): dados += "CNPJ. " + selecionado[0]:innerHTML + " - RAZÃO SOCIAL. " + selecionado[1]:innerHTML + " - NOME FANTASIA. " + selecionado[2];innerHTML + "\n"; } alert(dados); });
 {% load static %} <.DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'css/banco:css' %}"> </head> <body> <button id="visualizarDados">Visualizar Dados</button> <table border="1" class="dataframe" id="minhaTabela"> <thead> <tr style="text-align; right:"> <th></th> <th>CNPJ</th> <th>RazaoSocial</th> <th>NomeFantasia</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>00000000000</td> <td>xxxxxxxxxxxxx S/A</td> <td>xxxxxxxxxxx</td> </tr> <tr> <th>1</th> <td>111111111111</td> <td>yyyyyyyyyyyy S/A</td> <td>yyyyyyyyyyyyyy</td> </tr> </tbody> </table> <script src="https.//code.jquery.com/jquery-1.9.1.js"></script> <script src="{% static 'js/banco.js' %}" async> </script> </body> </html>

Instead of showing an alert, I would like to get this submit this var to my django's view.我不想显示警报,而是希望将此 var 提交到我的 django 视图。 I have no idea how to do a form in this situation, because the table is generated by pandas.to_html, so I don't know how to insert a form inside this table to send the rows selected's column[0], column[1] and column[2]我不知道在这种情况下如何做一个表格,因为表格是由 pandas.to_html 生成的,所以我不知道如何在这个表格中插入一个表格来发送所选行的 column[0]、column[1 ] 和列[2]

Note that Javascript is executed by the browser, while Django is run a server.请注意,Javascript 由浏览器执行,而 Django 运行在服务器上。 In order to enable communication, you will have to use XHR, aka "Ajax".为了启用通信,您必须使用 XHR,也就是“Ajax”。

On the JS side, you can create a HTTP request在JS端,可以创建一个HTTP请求

using the fetch API (async/Promise) https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API使用 fetch API (async/Promise) https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

or with an XMLHttpRequest (synchronous)https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest或使用 XMLHttpRequest(同步)https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

You should transform your variable to JSON and create a POST request to your Server您应该将变量转换为 JSON 并向您的服务器创建一个 POST 请求

const URL = http://yourServer.local/yourController
fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: {
      JSON.stringify(dados)
    }).then(result => {
       //what to do when the POST was successful and a possible answer was retrived
    })

on the Django end, please refer to the documentation on how to create a minimal controller and route to process a POST request https://docs.djangoproject.com/en/3.0/intro/tutorial04/在 Django 端,请参阅有关如何创建最小 controller 和路由以处理 POST 请求的文档 Z5E056C500A1C4B6A7110B50D807BADE45/3/3/0docs.introproject.com

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

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