antonnifo
antonnifo

Reputation: 46

How do I get an item id via a modal where the link that triggers it is within a for loop in Django

This is an online shop where the page that lists products has a small button that triggers a modal that display detailed info about the specific product. For me to be able to display this info I need to get this specific id back in the views so that I can fetch this specific product. Below is the html code

        {% if products %} 
         {% for item in products %}
          <!-- product-->
          <div class="col-xl-3 col-lg-4 col-sm-6">
            <div class="product">
              <div class="product-image">
                <div class="ribbon ribbon-info">Fresh</div><img class="img-fluid" src="{{item.image.url}}" alt="product"/>
                <div class="product-hover-overlay"><a class="product-hover-overlay-link" href="{{item.get_absolute_url}}"></a>
                  <div class="product-hover-overlay-buttons"><a class="btn btn-outline-dark btn-product-left" href="#"><i class="fa fa-shopping-cart"></i></a>
                  <a class="btn btn-dark btn-buy" href="{{item.get_absolute_url}}"><i class="fa-search fa"></i></a><a class="btn btn-outline-dark btn-product-right" href="{% url 'shop:modal_product_list' item.id %}" data-toggle="modal" data-target="#exampleModal"><i class="fa fa-expand-arrows-alt"></i></a>
                  </div>
                </div>
              </div>
              <div class="py-2">
                <p class="text-muted text-sm mb-1">{{item.products.sub_category}}</p>
                <h3 class="h6 text-uppercase mb-1"><a class="text-dark" href="{{item.get_absolute_url}}">{{item.name}}</a></h3><span class="text-muted">KSH {{item.discount_price}}</span>
              </div>
            </div>
          </div>
          <!-- /product-->             
         
         {% endfor %}
        
        {% else %}
        
        <p> No products at the moment. Please check back later</p>
        {% endif %}

I was hoping this href="{% url 'shop:modal_product_list' item.id %}" will come through...my view looks like this

def product_list(request, product_id=None,category_name=None,category_slug=None):

def get_category(name):
    return Category.objects.filter(name=name)

products   = PRODUCTS

category   = None
if category_slug:
    category = get_object_or_404(Category, slug=category_slug)
    products = products.filter(category=category)

category_name   = None
if category_name:
    category = get_object_or_404(Category, name=category_name)
    products = products.filter(category=category)

paginator = Paginator(products, 12)
page = request.GET.get('page')

print(product_id)

try:
    products = paginator.page(page)

except PageNotAnInteger:
    # If page is not an integer deliver the first page
    products = paginator.page(1)

except EmptyPage:
    # If page is out of range deliver last page of results
    products = paginator.page(paginator.num_pages)

return render(request,
  'shop/product/list.html',
  {
    "page": page,
    "products" : products,
    "Men"      : get_category('Gentlemen'),
    "Ladies"   : get_category('Ladies'),
    "Kids"     : get_category('Kids'),
    "category" : category,
    "category_name":category_name,
    "product_id" : product_id,

  })

and my urls file

from django.urls import path
from . import views

app_name = 'shop'

urlpatterns = [
    path('', views.index, name='home'),
    path('shop/', views.product_list, name='product_list'),
    path('shop/<str:category_name>/', views.product_list, name='product_list_by_category_name'),
    path('shop/<slug:category_slug>/', views.product_list, name='product_list_by_category'),
    path('shop/<int:product_id>/', views.product_list, name='modal_product_list'),
    path('<int:id>/<slug:slug>/', views.product_detail,
                                        name='product_detail'),
]

When I print the product_id as shown in the view I get None. If there is a way I can get to set that product_id then I will be able to finally get the product details. Below is the code of how the model which is triggered via template tags

{% if product_id %}
    {% display_modal product_id %}
{% endif %}

The big question is how do I get this product_id ...also any alternative ideas on how I can get the modal to display product detail info are welcome.

Upvotes: 1

Views: 279

Answers (0)

Related Questions