Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Django javascript redirect using id

We are trying to redirect to our checkout.html page which requires an id in its arguments to get to, but we are trying to get to it using Javascript.

In out store.html page

{% for item in product %}
            <div class="col-md-3">
                <div class="card" style="width: 90%; margin: 5px 5px;">
                    <a href="Product/{{item.id}}"><img src="{{item.image.url}}" class="card-img-top"></a>
                        <div class="cart-footer text-center">
                            <button onclick="redirect()">Order</button>
                        </div>

                </div>

            </div>
    


        {% endfor %}

<script>
function redirect() {
  var url = "{% url 'Checkout' %}";
  var id = $(this).item('id');
  window.location.href = url + "/" + id;
}
</script>

We know we can just use a regular button like

<button onclick="window.location.href='Checkout/{{item.id}}'">Order</button>

Which is how we had it before, but we're trying to get it through Javascript for future use How do you get a specific objects' id in javascript?

urls.py

    path('', views.Store, name= "Store"),
    path('Checkout/<id>', views.createlocation, name="Checkout"),
    path('Product/<id>', views.product_details, name="Product")
    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In Your Template :

.
.
{{ item|json_script:"item_obj" }}
<button onclick="redirect()">Order</button>
.
.

Then In your JS function

function redirect() {
  var url = "{% url 'Checkout' %}";
  const item = JSON.parse(document.getElementById('item_obj').textContent);
  window.location.href = url + "/" + item[id];
}

Checkout this example from doc

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!