I am developing an ecommerce website with django, I can not show on the product page (product_detail.html) the dates from Product_details model, from other models I can display dates, for example, from the Product_image model and from Category model I already take dates and display without any problems. It seems that I did all steps right, but I can not display the dates. Please help me.
urls.py:
from django.urls import path
from . import views
from .views import ProductListView
from django.conf.urls.static import static
urlpatterns = [
path('', ProductListView.as_view(), name='amd-home'),
path('product/<int:id>/', views.product_detail, name='product-detail'),
path('about/', views.about, name='amd-about'),
]
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=200)
parent_id = models.IntegerField(default=0)
description = models.TextField()
image = models.ImageField(upload_to='uploads/')
def __str__(self):
return f'{self.name}'
class Brand(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=400)
image = models.ImageField(upload_to='uploads/')
def __str__(self):
return f'{self.name}'
class Product(models.Model):
title = models.CharField(max_length=200)
image = models.ImageField(upload_to='uploads/', blank=True, null=True)
sku = models.CharField(max_length=200)
price = models.IntegerField(default=0)
price_old = models.IntegerField(default=0)
description = models.TextField()
status = models.BooleanField(default=False)
date_posted = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return f'{self.title}, {self.description}'
class Product_image(models.Model):
image = models.ImageField(upload_to='uploads/')
product = models.ForeignKey(Product, on_delete=models.CASCADE)
def __str__(self):
return f'{self.product.title} image'
class Product_details(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
size = models.CharField(max_length=50, blank=True, null=True, default=None)
color = models.CharField(max_length=50, blank=True, null=True, default=None)
video = models.CharField(max_length=200, blank=True, null=True, default=None)
internal_storage = models.CharField(max_length=50, blank=True, null=True, default=None)
ram = models.CharField(max_length=50, blank=True, null=True, default=None)
production_year = models.CharField(max_length=50, blank=True, null=True, default=None)
sim_count = models.CharField(max_length=50, blank=True, null=True, default=None)
sim_type = models.CharField(max_length=50, blank=True, null=True, default=None)
operation_system = models.CharField(max_length=100, blank=True, null=True, default=None)
operation_system_version = models.CharField(max_length=100, blank=True, null=True, default=None)
screen_size = models.CharField(max_length=50, blank=True, null=True, default=None)
resolution = models.CharField(max_length=50, blank=True, null=True, default=None)
back_camera = models.CharField(max_length=50, blank=True, null=True, default=None)
front_camera = models.CharField(max_length=50, blank=True, null=True, default=None)
battery = models.CharField(max_length=50, blank=True, null=True, default=None)
weigth = models.CharField(max_length=50, blank=True, null=True, default=None)
prosessor = models.CharField(max_length=50, blank=True, null=True, default=None)
security = models.CharField(max_length=50, blank=True, null=True, default=None)
guarantee = models.CharField(max_length=50, blank=True, null=True, default=None)
operator_prefix = models.CharField(max_length=50, blank=True, null=True, default=None)
wifi = models.BooleanField(blank=True, null=True, default=None)
allow_3G = models.BooleanField(blank=True, null=True, default=None)
allow_4G = models.BooleanField(blank=True, null=True, default=None)
nfc = models.BooleanField(blank=True, null=True, default=None)
gps = models.BooleanField(blank=True, null=True, default=None)
eye_recognition = models.BooleanField(blank=True, null=True, default=None)
waterproof = models.BooleanField(blank=True, null=True, default=None)
faceID = models.BooleanField(blank=True, null=True, default=None)
shockproof = models.BooleanField(blank=True, null=True, default=None)
def __str__(self):
return f'{self.product.title} details'
views.py
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from django.http import HttpResponse
from .models import Product, Product_image, Product_details
class ProductListView(ListView):
model = Product
template_name = 'product/home.html'
context_object_name = 'products'
ordering = ['-date_posted']
def product_detail(request, id):
product = Product.objects.get(pk=id)
photos = Product_image.objects.filter(product=product)
details = Product_details.objects.filter(product=product)
context = {'product':product,'photos':photos, 'details':details,}
return render(request, 'product/product_detail.html', context)
template product_detail.html Below is my template file, this is a very large file, so I only insert the element where I refer to this model. I want notice {{product.brand.name}} {{product.title}} is normally visible on screen, but {{details.ram}} / {{details.internal_storage}} is not visible.
<h2>{{ product.brand.name }} {{ product.title }} {{ details.ram }} / {{details.internal_storage}}</h2>
Product_details is a child table, so in your view, details is a queryset, it may contain 0 or multiple records
details = Product_details.objects.filter(product=product)
to display it, you need to loop through details
in your template
{% if details %}
<br>details
{% for detail in details %}
<br>{{ detail.ram }} / {{detail.internal_storage}}
{% endfor %}
{% endif %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.