繁体   English   中英

在我的 django slug 产品详细信息中获取 product.id

[英]Getting the product.id in my django slug product details

我在尝试在我的 product_details 模板中获取我的 product.id 时遇到问题。

这是我的views.py product_details

def product_details(request, slug):
    data = cartData(request)
    cartItems = data['cartItems']

    post = get_object_or_404(Product, slug=slug)
   
    context = {
        'post': post,
        'cartItems': cartItems,
    }      
    return render(request, 'e_commerce/product_details.html', context)

模型.py

class Product(models.Model):
    name = models.CharField(max_length=150)
    slug = models.SlugField(max_length=200, unique=True, null=True, blank=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    image = models.ImageField(upload_to='product_image', null=True, blank=True)
    digital = models.BooleanField(default=False,null=True, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE,default=1)
    description = models.CharField(max_length=250, default='', null=True, blank=True)
    size = models.CharField(max_length=200, null=True, blank=True, help_text='Input size of product')

网址.py

 path('<slug:slug>/', views.product_details, name='product_details'),#e_commerce details page

然后是 product_details 模板:

<div class="container">
                <div class="card">
                    <div class="container-fliud">
                        <div class="wrapper row">
                            <div class="preview col-md-6">
                                <div class="preview-pic tab-content">
                                    <div class="tab-pane active" id="pic-1"><img src="{{ post.image.url }}" /></div>
                                </div>
                            </div>
                            <div class="details col-md-6">
                                <h3 class="product-title">{% block title %} {{ post.name }} {% endblock title %}</h3>
                                <div class="rating">
                                    <div class="stars">

                                    </div>
                                </div>
                                <h5 class="product-description">{{post.description }}</h5>
                                <h4 class="price">price: <span>NGN {{ post.price }}</span></h4>
                                <h5 class="sizes">size: {{ post.size }}</h5>
                                <h5 class="lead">Category: {{ post.category }}</h5>
                                <hr>
                                  <button data-product=**{{product.id}}**  data-action="add" class="btn btn-outline-secondary add-btn update-cart">Add to Cart</button>
                                
                            </div>
                        </div>
                    </div>
                </div>
            </div>

除了获取 product.id 之外,一切都很好。

我需要它来使用我创建的 javascript function 以使用户能够通过单击模板中的“添加到购物车”按钮添加到购物车。

所以我后来想出了一个解决我的问题的方法。

我只需要包含{{ post.id }}即可获得我的product.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM