当我尝试在url.py中使用include关键字来反转匹配model.py reverse("products:detail", kwargs={"slug": self.slug}
中的网址时,它说:
Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
我在Django 2.0中看到,你必须为app_name提供命名空间。当我使用python manage.py startapp products
创建我的应用时,我的应用名称为products
。我的url.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from .views import hello_world, home_page, about_page, contact_page, login_page, register_page
urlpatterns = [
url(r'^hello/$', hello_world),
url(r'^$', home_page),
url(r'^admin/', admin.site.urls),
url(r'^about/$', about_page),
url(r'^contact/$', contact_page),
url(r'^login/$', login_page),
url(r'^register/$', register_page),
url(r'^products/', include(products, namespace='products')),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)`
我的产品/ model.py:
import random
import os
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.urls import reverse
from .utils import unique_slug_generator
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(filepath)
return name, ext
def upload_image_path(instance, filename):
new_filename = random.randint(0, 3000)
name, ext = get_filename_ext(filename)
final_filename = f'{new_filename}{ext}'#.format(new_filename=new_filename, ext=ext)
print(new_filename, final_filename, ext)
return f"products/{new_filename}/{final_filename}"
class ProductQuerySet(models.query.QuerySet):
def featured(self):
return self.filter(featured=True)
def active(self):
return self.filter(active=True)
class ProductManager(models.Manager):
def get_queryset(self):
return ProductQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().filter(active=True)
def features(self):
return self.get_queryset().filter(featured=True)
def get_by_id(self, id):
qs = self.get_queryset().filter(id=id)
if qs.count()==1:
return qs.first()
return None
class Product(models.Model): #Product_category
title = models.CharField(max_length=120)
slug = models.SlugField(blank=True, unique=True)
description = models.TextField()
price = models.DecimalField(decimal_places = 2, max_digits = 10, default=39.99)
image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
featured = models.BooleanField(default=False)
active = models.BooleanField(default=True)
objects = ProductManager()
def get_absolute_url(self):
# return "/products/{slug}".format(slug=self.slug)
return reverse("products:detail", kwargs={"slug": self.slug})
def __str__(self):
return self.title
def product_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(product_pre_save_receiver, sender=Product)
` 和我的目录树
├── db.sqlite3
├── maalamaal
│ ├── forms.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── manage.py
├── products
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ ├── models.py
│ ├── __pycache__
│ ├── templates
│ │ └── products
│ │ ├── detail.html
│ │ ├── featured-detail.html
│ │ ├── list.html
│ │ └── snippets
│ │ └── card.html
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── static_maalamaal
└── templates
├── auth
│ ├── login.html
│ └── register.html
├── base
│ ├── css.html
│ ├── js.html
│ └── navbar.html
├── base.html
├── contact
│ └── view.html
└── home_page.html
答案 0 :(得分:2)
看来,我必须创建一个urlconf和app_name的元组。
from django.apps import apps
products_name = apps.get_app_config('products').verbose_name
然后,
url(r'^products/', include(('products.urls', products_name), namespace='products')),