Создание тега продукта работает в одиночку, но если я добавлю какую-либо запись в продукт (которая должна быть связана с тегом продукта), это приведет к ошибке.
Вот URL из приложения:
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.views.generic import TemplateView
from django.views.generic.detail import DetailView
from main import views
from main import forms
from main import models
path(
"products/<slug:tag>/",
views.ProductListView.as_view(),
name="products",
),
Вот models.py:
class ProductTag(models.Model):
name = models.CharField(max_length=40)
slug = models.SlugField(max_length=48)
description = models.TextField(blank=True)
active = models.BooleanField(default=True)
objects = ProductTagManager()
def __str__(self):
return self.name
def natural_key(self):
return (self.slug,)
class ActiveManager(models.Manager):
def active(self):
return self.filter(active=True)
class Product(models.Model):
name = models.CharField(max_length=40)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
slug = models.SlugField(max_length=48)
tags = models.ManyToManyField(ProductTag, blank=True)
active = models.BooleanField(default=True)
in_stock = models.BooleanField(default=True)
date_updated = models.DateTimeField(auto_now=True)
objects = ActiveManager()
def __str__(self):
return self.name
class ProductImage(models.Model):
product = models.ForeignKey(
Product, on_delete=models.CASCADE
)
image = models.ImageField(upload_to="product-images")
thumbnail = models.ImageField(
upload_to="product-thumbnails", null=True
)
Вот views.py:
class ProductListView(ListView):
template_name = "main/product_list.html"
paginate_by = 4
def get_queryset(self):
tag = self.kwargs["tag"]
self.tag = None
if tag != "all":
self.tag = get_object_or_404(
models.ProductTag, slug=tag
)
if self.tag:
products = models.Product.objects.active().filter(
tags=self.tag
)
else:
products = models.Product.objects.active()
return products.order_by("name")
Страница ошибки выглядит так:
NoReverseMatch at /products/romance/
Reverse for 'product' not found. 'product' is not a valid view
function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/products/romance/
Django Version: 3.0.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'product' not found. 'product' is not a valid view
function or pattern name.
Exception Location:
/home/kash/.local/share/virtualenvs/booktimes-
7D7i9gyr/lib/python3.7/site-packages/django/urls/resolvers.py in
_reverse_with_prefix, line 677
Python Executable:
/home/kash/.local/share/virtualenvs/booktimes-7D7i9gyr/bin/python
Python Version: 3.7.3
Python Path:
['/home/kash/py/booktimes',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/kash/.local/share/virtualenvs/booktimes-
7D7i9gyr/lib/python3.7/site-packages']
Server time: Mon, 6 Apr 2020 21:46:14 +0000
Error during template rendering
In template /home/kash/py/booktimes/main/templates/base.html, error
at line 8
Reverse for 'product' not found. 'product' is not a valid view
function or pattern name.
{% load static %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to
fit=no">
<link
rel="stylesheet"
href="{% static "css/bootstrap.min.css" %}">
<title>BookTime</title>
<!-- Booktime version: {{ VERSION }} -->
<script charset="utf-8">
var tracker_id = {{ GA_TRACKER_ID }};
Я думаю, что проблема в где-то в «соединении» между моделями, я, к сожалению, не смог найти ошибку, даже сделав тщательную проверку, но не смог ее обнаружить ............