Всем доброго времени суток!
Не могу найти причину почему таблица не отображается на сайте, возможно отсутствует подключение к базе.
Где искать причину?
settings.py
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": os.environ.get("DB_NAME", "postgres"),
"USER": os.environ.get("DB_USER", "postgres"),
"PASSWORD": os.environ.get("DB_PASSWORD"),
"HOST": os.environ.get("DB_HOST", "localhost"),
"PORT": os.environ.get("DB_PORT", "5432"),
}
}
if os.environ.get("USE_SQLITE", "true").lower() == "true":
DATABASES["default"] = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
models.py
class Item(models.Model):
url = models.CharField(
unique=True, max_length=300, verbose_name="Ссылка на страницу товара"
)
provider = models.ForeignKey(
Provider,
null=True,
blank=True,
verbose_name="Поставщик",
on_delete=models.SET_NULL,
)
name = models.CharField(
max_length=300, null=True, blank=True, verbose_name="Название товара"
)
image_url = models.CharField(
max_length=300,
null=True,
blank=True,
verbose_name="Ссылка на изображение",
)
brand = models.ForeignKey(
Brand,
null=True,
blank=True,
verbose_name="Бренд",
on_delete=models.SET_NULL,
)
vendor_code = models.IntegerField(
null=True, blank=True, verbose_name="Артикул"
)
rating = models.IntegerField(null=True, blank=True, verbose_name="Рейтинг")
reviews = models.IntegerField(
null=True, blank=True, verbose_name="Количество отзывов"
)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
null=True,
blank=True,
verbose_name="Цена без скидки",
)
price_with_discount = models.DecimalField(
max_digits=10,
decimal_places=2,
null=True,
blank=True,
verbose_name="Цена со скидкой",
)
quantity = models.IntegerField(
null=True, blank=True, verbose_name="Доступно"
)
categories = models.ManyToManyField(
Category, blank=True, verbose_name="Категории"
)
quantity_of_purchased = models.IntegerField(
null=True, blank=True, verbose_name="Количество покупок"
)
revenue = models.DecimalField(
max_digits=35,
decimal_places=2,
null=True,
blank=True,
verbose_name="Выручка",
)
class Meta:
verbose_name = "Товар"
verbose_name_plural = "Товары"
def __str__(self):
return f"{self.name} {self.brand} [{self.vendor_code}]"
views.py
class ItemListView(LoginRequiredMixin, SubscriptionRequiredMixin, FilterView):
template_name = "item_list.html"
paginate_by = 10
model = Item
filterset_class = ItemFilter
filter_backends = DjangoFilterBackend
def get_queryset(self, **kwargs):
qs = ItemFilter(self.request.GET, Item.objects.all()).qs
return qs
def get_context_data(self, **kwargs):
table_headers = [
{"id": "name", "name": "Товар"},
{"id": "brand", "name": "Бренд"},
{"id": "provider", "name": "Поставщик"},
{"id": "rating", "name": "Рейтинг"},
{"id": "reviews", "name": "Отзывы"},
{"id": "price_with_discount", "name": "Цена"},
{"id": "quantity", "name": "Доступно"},
{"id": "categories", "name": "Категории"},
{"id": "quantity_of_purchased", "name": "Куплено"},
{"id": "revenue", "name": "Выручка"},
]
ordering = self.request.GET.get("order_by", None)
for header in table_headers:
query = QueryDict(self.request.GET.urlencode(), mutable=True)
if ordering == f"-{header['id']}":
query["order_by"] = f"{header['id']}"
elif ordering != header["id"]:
query["order_by"] = f"-{header['id']}"
else:
query["order_by"] = ""
query["page"] = 1
header["url"] = f"?{query.urlencode()}"
if ordering:
match_obj = re.match(r"-?(.+)", ordering)
order_field = match_obj.groups()[0]
order_field_index = next(
(
index
for (index, d) in enumerate(table_headers)
if d["id"] == order_field
),
None,
)
table_headers[order_field_index]["active"] = True
table_headers[order_field_index]["down"] = ordering != order_field
context = super().get_context_data(**kwargs)
context["table_headers"] = table_headers
return context
def get_ordering(self):
fields = [
"id",
"provider",
"name",
"brand",
"vendor_code",
"rating",
"reviews",
"price",
"price_with_discount",
"quantity",
"categories",
"quantity_of_purchased",
"revenue",
]
ordering = self.request.GET.get("order_by", "id")
match_obj = re.match(r"-?(.+)", ordering)
if match_obj and (match_obj.groups()[0] in fields):
field = match_obj.groups()[0]
if field == ordering:
return [F(field).asc(nulls_first=True)]
else:
return [F(field).desc(nulls_last=True)]
return "id"
Все работало без проблем до Backup и переноса проекта на другой сервер.