'core'不是注册的命名空间 Django



这是我第一次得到这个错误,而试图创建一个slug。似乎找不到原因,这里也没有其他答案。用url编辑答案

models.py:

from django.db import models
from django.conf import settings
from django.shortcuts import reverse
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2, default="Sport")
slug = models.SlugField(default="")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("core:productpage", kwargs={
'slug': self.slug
})

views.py:

from django.shortcuts import render
from django.contrib import admin
from .models import *
from django.views.generic import ListView, DetailView
def index(request):
context = {'items': Item.objects.all()}
return render(request, 'ecommerceapp/index.html', context) #returns the index.html template
def shop(request):
context = {'items': Item.objects.all()}
return render(request, 'ecommerceapp/shop.html', context)
def about(request):
return render(request, 'ecommerceapp/about.html')
def blog(request):
return render(request, 'ecommerceapp/blog.html')
def contact(request):
return render(request, 'ecommerceapp/contact.html')
def productpage(request):
return render(request, 'ecommerceapp/product-single.html')

urls . py:

from . import views
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('', views.index, name='index'),
path('shop/', views.shop, name="shop"),
path('about/', views.about, name="about"),
path('blog/', views.blog, name="blog"),
path('contact/', views.contact, name="contact"),
path('product-single/<slug>/', views.productpage, name="productpage")
]

谢谢你的回答和帮助!

在您的urls.py中,您需要指定一个命名空间,如下所示:

from . import views
from django.contrib import admin
from django.urls import path
app_name = 'core' # This is the namespace so you can reverse urls with core:*
urlpatterns = [
path('', views.index, name='index'),
path('shop/', views.shop, name="shop"),
path('about/', views.about, name="about"),
path('blog/', views.blog, name="blog"),
path('contact/', views.contact, name="contact"),
path('product-single/<slug>/', views.productpage, name="productpage")
]

最新更新