特定商店的产品未出现在在线商城中
Products of a particular shop not showing up in an online 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 %}
下面的页面应该显示商店的详细信息并显示该商店内的所有产品,删除了一些代码以使其最小化
店铺-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 但我无法实施
这个对我有用
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)
正在开发商城,首页显示商城所有店铺。
当访问者点击不同的商店时,我无法显示特定商店的产品
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 %}
下面的页面应该显示商店的详细信息并显示该商店内的所有产品,删除了一些代码以使其最小化
店铺-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 但我无法实施
这个对我有用 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)