A
A
Alexander Bondarenko2020-07-05 19:12:32
Django
Alexander Bondarenko, 2020-07-05 19:12:32

Why does the NoReverseMatch at /shop/ error occur?

I searched the Internet for the error 'shop' is not a registered namespace, I seem to do everything this way, but the error remains, how to fix it?
models.py

from django.db import models
from django.urls import reverse

# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, unique=True)
    image = models.FileField(upload_to='category/', blank=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'

    def get_absolute_url(self):
        return reverse('shop:product_list_by_category',args=[self.slug])
            
    def __str__(self):
        return self.name

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products' , on_delete=models.CASCADE)
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),)

    def get_absolute_url(self):
        return reverse('shop:product_detail',args=[self.id, self.slug])

    def __str__(self):
        return self.name

urls.py
from django.urls import path, include
from . import views



urlpatterns=[
    path('',views.product_list, name='product_list'),
    path('<str:category_slug>/',views.product_list, name='product_list_by_category'),
    path('<int:id>/<slug:slug>/',views.product_detail,name='product_detail'),

views.py
from django.shortcuts import render, HttpResponse, get_object_or_404
from .models import Category, Product

# Create your views here.

def index(request):
    return render(request, 'shop/product/index.html')

def product_list(request,category_slug=None):
    category = None
    categories= Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=category)


    return render(request,'shop/product/index.html',{'category': category,'categories': categories,'products': products})

def product_detail(request,id,slug):
    product = get_object_or_404(Product,id=id,slug=slug,available=True)
    return render(request, 'shop/product/detail.html', {'product': product})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-07-05
@bond_1013

In the main urls.py, in the place where the shop file is added, you need to add a namespace to it https://docs.djangoproject.com/en/3.0/topics/http/...
namespace='shop'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question