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

145
Views
¿Cómo pasar algunos datos a Django views.py?

Quiero enviar la identificación del producto a mis vistas.

Plantillas

 <div class="products__header--search-result"> <ul> {% for product in products %} <li onclick="change('{{ product.id }}')"><a href="#">{{ product.name }}</a></li> {% endfor %} </ul> </div> <script> function change(foo) { $.ajax({ url: {% url 'dashboard' %}, data: { 'foo': foo, }, }); } </script>

vistas.py

 myRequets = request.GET.get('foo')

Pero myRequets devuelve 'Ninguno'

¿Cómo puedo arreglar esto? ¿Hay una mejor manera de pasar valores a views.py?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No necesita JS para hacer esto porque Django permite parámetros en las URL:

 # URLconf from django.urls import path from . import views urlpatterns = [ path('product/', views.product), path('product/<int:product_id>/', views.product), ]

vistas.py

 # View (in product/views.py) def product(request, product_id=None): products = None if product_id: # Output the appropriate page of product entry, according to product_id. return ... # Output the appropriate page when product_id is None return ...

Fuente:https://docs.djangoproject.com/en/3.2/topics/http/urls/#example (adaptado)

Luego en tu plantilla:

 {% if products %} <div class="products__header--search-result"> <ul> {% for product in products %} <li><a href="/product/{{ product_id }}/">{{ product.name }}</a></li> {% endfor %} </ul> </div> {% endif %}
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!