Answer the question
In order to leave comments, you need to log in
Django, did it properly bind the models and render?
The problem is this: there are three models in mysite.models, Category - parent, Brand - child, Card - respectively, even lower. Have I connected them correctly for proper display, here is the
models.py code.
from django.db import models
from django.urls import reverse
class Category(models.Model):
name_cat = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, unique=True, db_index=True, verbose_name='URL')
class Meta:
ordering = ('name_cat',)
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __str__(self):
return self.name_cat
def get_absolute_url(self):
return reverse('cats', kwargs={'cat_slug': self.slug})
class Brand(models.Model):
category = models.ForeignKey(Category, on_delete=models.PROTECT, null=True,)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, verbose_name='URL')
class Meta:
ordering = ('name',)
verbose_name = 'Бренд'
verbose_name_plural = 'Бренды'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('brand', kwargs={'brand_slug': self.slug})
class Card(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.PROTECT)
boots_model = models.CharField(max_length=100, db_index=True, verbose_name='Модель бутс')
description = models.TextField(verbose_name='Описание')
slug = models.SlugField(max_length=200, unique=True, db_index=True, verbose_name='URL')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Цена')
image = models.ImageField(upload_to="photos/%Y/%m/%d/", blank=True)
created = models.DateTimeField(auto_now_add=True, db_index=True)
updated = models.DateTimeField(auto_now=True)
size = models.DecimalField(max_digits=4, decimal_places=1, verbose_name='Размер')
color = models.CharField(max_length=100, db_index=True, verbose_name='цвет')
class Meta:
ordering = ('-created',)
verbose_name = 'Объявление'
verbose_name_plural = 'Объявления'
def __str__(self):
return self.boots_model
def get_absolute_url(self):
return reverse('card', kwargs={'card_slug': self.slug})
views.py
___________________________________________________________________________________________________
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from .models import Category, Brand, Card
def index(request):
brands = Brand.objects.all()
categories = Category.objects.all()
cards = Card.objects.all()
context = {
'categories': categories,
'brands': brands,
'cards': cards
}
return render(request, 'mysite/index.html', context)
def show_categories(request, cat_slug):
brands = Brand.objects.filter(category__slug=cat_slug)
categories = Category.objects.all()
cards = Card.objects.filter(category__slug=cat_slug)
context = {
'categories': categories,
'brands': brands,
'cards': cards
}
return render(request, 'mysite/cats.html', context)
def show_brands(request, brand_slug):
brands = Brand.objects.filter(slug=brand_slug)
categories = Category.objects.all()
current_brand = Brand.objects.get(slug=brand_slug)
cards = Card.objects.filter(brand__slug=brand_slug)
context = {
'categories': categories,
'brands': brands,
'cards': cards,
'current_brand': current_brand
}
return render(request, 'mysite/brand.html', context)
def show_cards(request, card_slug):
brands = Brand.objects.all()
categories = Category.objects.all()
cards = Card.objects.get(slug=card_slug)
context = {
'categories': categories,
'brands': brands,
'cards': cards,
}
return render(request, 'mysite/card.html', context)
def about(request):
categories = Category.objects.all()
return render(request, 'mysite/about.html', {'categories': categories})
urls.py
_____________________________________________________________________________________________
urls. from django.urls import path
from . import views
from .views import index
urlpatterns = [
path('', views.index, name='home'),
path('about/', views.about, name='about'),
path('card/<slug:card_slug>/', views.show_cards, name='card'),
path('cats/<slug:cat_slug>/', views.show_categories, name='cats'),
path('brand/<slug:brand_slug>/', views.show_brands, name='brand'),
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question