title: How to pass data using javascript in html file, Django template?
body: <p>I am trying to fetch the data (details of the products ordered) from checkout page of HTML I designed. Tried implementing it using javascript but the values are empty when rendered through views.</p>
  <p>checkout.html -</p>
  <pre><code>  &lt;div class=&quot;container text-center&quot;&gt;
        &lt;h1&gt;Checkout&lt;/h1&gt;
    &lt;/div&gt;
  &lt;/header&gt;
  &lt;div class=&quot;col my-4&quot;&gt;
    &lt;h2&gt; Your Cart Items &lt;/h2&gt;
      &lt;table class=&quot;table table-striped&quot;&gt;
          &lt;thead&gt;
            &lt;tr&gt;
              &lt;th&gt;Product Name&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
              &lt;th&gt;Quantity&lt;/th&gt;
              &lt;th&gt;Price&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
            {% for item in cart_items %}
              &lt;tr&gt;
                &lt;td&gt;{{ item.product.name }}&lt;/td&gt;
                &lt;td&gt;{{ item.product.description }}&lt;/td&gt;
                &lt;td&gt;{{ item.quantity }}&lt;/td&gt;
                &lt;td&gt;{{ item.product.price }}&lt;/td&gt;
              &lt;/tr&gt;
            {% endfor %}
        &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;Total&lt;/td&gt;
              &lt;td&gt;{{ total }}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
  
  &lt;div class=&quot;col my-1&quot;&gt;
  
        &lt;h2&gt;Enter Address &amp; Other Details:&lt;/h2&gt;
        &lt;form method=&quot;post&quot; action=&quot;/thankyou/&quot;&gt;{% csrf_token %}
          &lt;input type=&quot;hidden&quot; name=&quot;itemsJson&quot; id='itemsJson'&gt;
          &lt;div class=&quot;form-row&quot;&gt;
              &lt;div class=&quot;form-group col-md-6&quot;&gt;
                  &lt;label for=&quot;inputname&quot;&gt;Name&lt;/label&gt;
                  &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name&quot; name=&quot;name&quot; placeholder=&quot;Name&quot;&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-row&quot;&gt;
              &lt;div class=&quot;form-group col-md-6&quot;&gt;
                  &lt;label for=&quot;inputCity&quot;&gt;City&lt;/label&gt;
                  &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;city&quot; name=&quot;city&quot;&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-group col-md-6&quot;&gt;
              &lt;label for=&quot;inputphone&quot;&gt;Phone Number&lt;/label&gt;
              &lt;input type=&quot;tel&quot; class=&quot;form-control&quot; id=&quot;phone&quot; name=&quot;phone&quot;&gt;
          &lt;/div&gt;
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Place Order&lt;/button&gt;
        &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
    $('#itemsJson').val(JSON.stringify(cart_items));
  &lt;/script&gt;
  </code></pre>
  <p>views.py</p>
  <pre><code>def thankyou(request):
      if request.method == &quot;POST&quot;:
          items_json = request.POST.get('itemsJson', '')
          name = request.POST.get('name', '')
          city = request.POST.get('city', '')
          phone = request.POST.get('phone', '')
          print (request.POST)
          
          order = Order(items_json= items_json, full_name=name, town_or_city=city, phone_number=phone)
          order.save()
          id = order.order_id
          return render(request, 'thankyou.html', {'thank':thank, 'id':id,})
  </code></pre>
  <p>The variable (id=itemsJson) is empty when rendered in views. This will be saved along with the form items to create a db entry.
  The other form items are populated as expected.</p>