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

260
Views
Django javascript redireccionar usando id

Estamos tratando de redirigir a nuestra página checkout.html que requiere una identificación en sus argumentos para llegar, pero estamos tratando de llegar usando Javascript.

En la página store.html

 {% 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>

Sabemos que podemos usar un botón normal como

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

Así es como lo teníamos antes, pero estamos tratando de obtenerlo a través de Javascript para uso futuro. ¿Cómo se obtiene la identificación de un objeto específico en 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

En su plantilla:

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

Entonces en tu función JS

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

Mira este ejemplo de 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!