【问题标题】:Products of a particular shop not showing up in an online mall特定商店的产品未出现在在线商城中
【发布时间】:2021-05-22 02:12:33
【问题描述】:

正在开发一个mall,首页显示mall里的所有店铺。

当访问者点击不同的商店时,我无法显示特定商店的产品

models.py

from django.db import models
from django.contrib.auth.models import User

class Shop(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, default=None )
    name = models.CharField(max_length=200)


class Product(models.Model):
    shop_name = models.ForeignKey(Shop, on_delete=models.CASCADE)
    product_name = models.CharField(max_length=200)

以下是我在应用中的看法

views.py

from django.shortcuts import render, get_object_or_404
from .models import Shop, Product


def index(request):
    shops = Shop.objects.all()
    context = {"shops":shops}
    return render(request, 'index.html',context)


def shop_details(request, pk):
    shop_details = get_object_or_404(Shop, pk)
    products = Product.objects.filter(id=pk)
    context = {'shop_details':shop_details, 'products':products}
    return render(request, 'shop_detail.html', context)


def shop_product_details(request):
    return render(request, 'shop-product-detail.html', {})

以下是我的网址 *urls.py

from django.urls import path
from . import views


urlpatterns = [
    
    path('all_shops/', views.index, name='index'),
    path('shop_details/<int:pk>/', views.shopdetails, name='shop_details'),
    path('shop_product_details/', views.shop_product_details, name='shop_product_details'),
]

以下是我的模板 index.html

{% for shop in shops %}
    <div>
        <div>
            <a href="{% url 'shop_details' shop.pk %}">
                <img src="/uploads/{{ shop.shop_logo }}" alt="{{ shop.shop_name }}">
            </a>
        </div>
        <div>
            <article>
                <h4 >
                    < a href = "{% url 'shop_details' shop.pk %}" > {{shop.shop_name}} < / a >
                </h4>
                <div>
                    <a href="{% url 'shop_details' shop.pk %}"> Visit Shop</a>
                </div>
            </article>
        </div>
    </div>
{% endfor %}

以下页面应显示商店的详细信息并显示该商店内的所有产品,删除了一些代码以使其最小化

shop-detail.html

{% for product in products %}
    <div>
        {{product.shop_name}}
        {{product.product_name}}
        {{product.product_added_on}}
        {{product.product_description}}
        <a href="{% url 'shop_product_details' %}"> View Product</a>
    </div>
    {% empty %}
        <p> No Products available</p>
{%endfor % }

我希望使用这里显示的多租户 https://www.youtube.com/watch?v=NsWlUMTfIFo 但我无法实现

【问题讨论】:

  • 请参阅How to Ask。因为这个问题没有显示现有的研究,也没有提到具体问题(错误、意外输出等)
  • @Seth 我哪里出错了,已经完成了如何提问。我应该如何重组我的问题
  • “因为它是这个问题显示没有现有的研究并且没有提到具体问题(错误、意外输出等)”提出问题的主要前提是您遇到了具体问题。 “我该怎么做”问题不是具体问题。
  • @Seth,我明白你让我改写问题,错误是商店的产品没有出现
  • 好的,现在你能提供一个minimal reproducible example来显示问题吗?

标签: python django e-commerce multi-tenant


【解决方案1】:

这个对我有用 views.py

def shop(request, pk):
    template = 'mall/shop-detail.html'
    
    # Display products in a single shop
    shop = Shop.objects.get(id=pk)
    products = shop.product_set.all()
    
    context = {'shop':shop, 'products':products,}
    return render(request, template, context)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多