【问题标题】:Getting the product.id in my django slug product details在我的 django slug 产品详细信息中获取 product.id
【发布时间】:2022-01-18 17:10:30
【问题描述】:

我在尝试在我的 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)

models.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')

urls.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 函数,以使用户能够通过单击模板中的 '添加到购物车' 按钮来添加到购物车。

【问题讨论】:

  • 我没有看到您在视图上下文中传递“产品”。您只传递了“post”和“cartItems”
  • 我用post = get_object_or_404(Product, slug=slug)@d1spstack 查询了产品模型,我猜是这样;这就是我将它传递给我的视图上下文的原因。
  • 但是您的上下文没有一个名为“产品”的变量。您将查询存储在名为“post”的变量中,然后尝试通过名为“product”的变量访问模板中的查询。
  • @d1spstack 是的,我后来意识到了这一点。我一直在研究它,而不是产品,我使用的是 post。谢谢

标签: javascript python html django templates


【解决方案1】:

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

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

【讨论】:

  • 也许下次您可能想将产品对象命名为 product,而不是 post...
  • @AKX 当然。我会记住这一点。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-01-03
  • 2022-01-22
  • 2018-01-23
  • 2012-05-28
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多