Home » Javascript » python – How to pass a javascript array in an html file to another html file in django and also redirect to the new html file

python – How to pass a javascript array in an html file to another html file in django and also redirect to the new html file

Posted by: admin February 26, 2020 Leave a comment

Questions:
 var resulttable = "<table border=1>";
    resulttable += "<tr>";
        resulttable += "<th>"+heading1+"</th>";
        resulttable += "<th>"+heading2+"</th>";
        resulttable += "<th>"+heading3+"</th>";
        resulttable += "<th>"+heading4+"</th>";
    resulttable += "</tr>";
    for(var i=0; i<angles.length; i++) {
        resulttable += "<tr>";
            resulttable += "<td>"+angles[i]+"</td>";
            resulttable += "<td>"+expert_angle[i]+"</td>";
            resulttable += "<td>"+user_angle[i]+"</td>";
            resulttable += "<td>"+diff[i]+"</td>";

        resulttable += "</tr>";
    }

Above is the javascript string in an html file. I want to pass this string into another another html file and display its contents to the user. I cant display it in the current page as it already has many contents. I want to know how to pass the javascript array to another file and how to redirect to the new html file after passing of the string

Any help would be appreciated and thanks in advance

How to&Answers:

I have an idea, but it is an Idea!
send this js array with AJAX or any other tool, but like a form with one input text field to the backend, to a views.py function
inside that function you’re able to take the text, and again pass it to a new html file and render.

<form id="js-table-form" method="post"  action="{% url 'js-table' %}" >
     <input type="hidden" id="js-table-holder" name="tablejs" />
</form>
<a href="javascript:{}" id="js-form-submit" > click here! </a>
<script>
    $("#js-form-submit").on('click', function (){
        $("#js-table-holder").val(" here must be your table data ");
        $("#js-table-form").send();
   });
</script>

in urls.py -> urlpatterns:

path('js-table', views.js_table, name="js-table")

in views.py:

def js_table(request):
    table = str()
    if request.method == 'POST':
         table = request.POST.get('tablejs') # the hidden inputs name attribute value!
    return render (request, "table_page.html", {"table": table})

in table_page.html:

{{ table|safe }}

you could catch every part of the table in an input field separately and then again in the last template rebuild the table part by part