Am developing a mall, the homepage displays all shops in the mall.
I am unable to show products of a specific shop when a visitor clicks on different shop
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)
Below is my views in the app
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', {})
below are my urls *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'),
]
Below are my templates 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 %}
Below page should show details of the shop and show all products within this shop, removed some codes to make it minimal
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 % }
I would have wished to use multi tenant as shown here https://www.youtube.com/watch?v=NsWlUMTfIFo but am unable to implement
This one worked for me 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)