I am working on a practice project to create 3 separate pages that is Brand Page, Car Model Page and Car Model Variant detail page.
I am working on a project with three models car brands, car model, car model variants When an user opens the Car model page they should see the list of variants in that car model
class BrandName(models.Model):
FullName = models.CharField(max_length=100)
Slogan = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
def __str__(self):
return f"{self.FullName}"
class CarModel(models.Model):
Brand = models.ForeignKey(BrandName, on_delete=models.CASCADE, null=True,
related_name="brands")
ModelName = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
def __str__(self):
return f"{self.ModelName}"
class CarModelVariant(models.Model):
ModelVariantName = models.CharField(max_length=100)
Brandname = models.ForeignKey(BrandName, on_delete=models.CASCADE, null=True,
related_name="brands1")
Model1 = models.ForeignKey(CarModel, on_delete=models.CASCADE, null=True,
related_name="model1")
Doors = models.IntegerField
SeatingCapacity = models.IntegerField
def __str__(self):
return f"{self.ModelVariantName}"
In views.py of the app:
from django.shortcuts import render
from django.http import HttpResponse
from . models import CarModelVariant, CarModel, BrandName
from django.views.generic import ListView, DetailView
class CarModelVariantView(ListView):
template_name = "carlist.html"
model = CarModelVariant
def get_queryset(self):
list_model = super().get_queryset()
data = list_model.filter(Model1=CarModel_instance)
return data
In urls.py of the app
from django.urls import path
from . import views
urlpatterns = [
path("<slug:slug>", views.CarModelVariantView.as_view())
]
I am facing a blank page while loading carlist.html template:
{% extends "base.html" %}
{% block title%}
Car Brand Variants List
{% endblock%}
{% block content %}
<ul>
{% for variant in data %}
<li>{{ variant }}</li>
{% endfor %}
</ul>
{% endblock %}
You do not see the list of elements because you are not using the naming correctly for the CarModelVariant model list. By default, the variable that stores this list is object_list.
That is, if you want to iterate over the CarModelVariant list, you should use the following construction
if you want to use your variable for the list, you must specify the context_object_name attribute
UPD
Using attributes in classes with capital letters is considered bad form.