I am creating a website for the sale of goods, and for each product I create a card based on data from the database. When a new card appears, a dynamic page based on the same database data should be automatically connected to it. The page is created, but the data from the model is not loaded. I watched the Django guide, and it showed this method using DetailView, from where I took the code. Please help me, I don't understand what the problem is.
My code:
views.py:
class car(DetailView):
model = inventory
template_name = 'main/cars.html'
context_object_name = 'inventory'
urls.py:
from . import views
urlpatterns = [
path('inventory/<int:pk>', views.car.as_view(), name='cars'),
]
cars.html:
{% extends 'main/layout.html' %}
{% block main %}
{% for el in invent %}
<div class="main-card ">
<img src = '{{ el.img_1 }}' style="">
<h3 style="">{{ el.name }}</h3>
<h4 style="">{{ el.rent }}</h4>
<button><a href="{% url 'cars' el.id %}">Details</a></button>
</div>
{% endfor %}
{% endblock %}
models.py:
class inventory(models.Model):
name = models.CharField('Name', max_length=100)
type = models.CharField('Type of car', max_length=6)
img_1 = models.ImageField(upload_to='sql_imgs/')
img_2 = models.ImageField(upload_to='sql_imgs/')
img_3 = models.ImageField(upload_to='sql_imgs/')
img_4 = models.ImageField(upload_to='sql_imgs/')
img_5 = models.ImageField(upload_to='sql_imgs/')
img_6 = models.ImageField(upload_to='sql_imgs/')
img_7 = models.ImageField(upload_to='sql_imgs/')
img_8 = models.ImageField(upload_to='sql_imgs/')
MSRP = models.CharField('msrp', max_length=40)
Purchase = models.CharField('purchase', max_length=40)
rent = models.CharField('rent', max_length=40)
specs = models.TextField('specs')
text = models.TextField('About car')
def str(self):
return self.name
class Meta:
verbose_name = 'Inventory'
verbose_name_plural = 'Inventory'
I've read a lot of articles, and I haven't found anything worthwhile. I hope I can find the answer here.
Please update your code like this:
models.py
from django.db import models
class Inventory(models.Model):
name = models.CharField('Name', max_length=100)
type = models.CharField('Type of car', max_length=6)
img_1 = models.ImageField(upload_to='sql_imgs/')
img_2 = models.ImageField(upload_to='sql_imgs/')
img_3 = models.ImageField(upload_to='sql_imgs/')
img_4 = models.ImageField(upload_to='sql_imgs/')
img_5 = models.ImageField(upload_to='sql_imgs/')
img_6 = models.ImageField(upload_to='sql_imgs/')
img_7 = models.ImageField(upload_to='sql_imgs/')
img_8 = models.ImageField(upload_to='sql_imgs/')
MSRP = models.CharField('msrp', max_length=40)
Purchase = models.CharField('purchase', max_length=40)
rent = models.CharField('rent', max_length=40)
specs = models.TextField('specs')
text = models.TextField('About car')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Inventory'
verbose_name_plural = 'Inventories'
views.py
from django.views.generic.detail import DetailView
from .models import Inventory
class CarDetailView(DetailView):
model = Inventory
template_name = 'main/cars.html'
context_object_name = 'inventory'
urls.py
from django.urls import path
from .views import CarDetailView
urlpatterns = [
path('inventory/<int:pk>/', CarDetailView.as_view(), name='cars'), # Added trailing slash for best practice
]
cars.html
{% extends 'main/layout.html' %}
{% block main %}
<div class="main-card">
<img src="{{ inventory.img_1.url }}" alt="{{ inventory.name }}">
<h3>{{ inventory.name }}</h3>
<h4>{{ inventory.rent }}</h4>
<p>{{ inventory.text }}</p>
</div>
{% endblock %}
Please update your code like this and lets see what happens next to fix it as needed.