在include()中指定名称空间而不提供app_name。提供app_name也不起作用

时间:2019-12-17 19:59:03

标签: python django

from django.conf import  settings
from django.conf.urls.static import static

from django.conf.urls import  url, include
from django.contrib import  admin
from django.views.generic import TemplateView


from carts.views import cart_home

from .views import  home_page, about_page, contact_page, login_page, register_page
app_name = 'products'
urlpatterns = [
    url(r'^$', home_page, name='home'),
    url(r'^about/$', about_page, name='about'),
    url(r'^contact/$', contact_page, name='contact'),
    url(r'^login/$', login_page,name='login'),
    #url(r'^cart/', include("carts.urls"),
    url(r'^register/$', register_page, name='register'),
    url(r'^bootstrap/$', TemplateView.as_view(template_name='bootstrap/example.html')),
    url(r'^products/', include("products.urls", namespace='products')),

    url(r'^admin/', admin.site.urls),
]

请帮助。我收到此错误:

  

'在include()中指定名称空间而不提供app_name'   django.core.exceptions.ImproperlyConfigured:在以下位置指定名称空间   不支持include()而不提供app_name。设置   随附模块中的app_name属性,或传递2元组   包含模式列表和app_name。

产品\ urls.py:

from django.conf.urls import url

    from .views import(
            ProductListView,
            ProductDetailSlugView,
        )

    urlpatterns = [
        url(r'^$', ProductListView.as_view(), name='list'),
        url(r'^(?P<slug>[\w-]+)/$', ProductDetailSlugView.as_view(), name='detail'),
    ]

products \ models.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):
    basename = os.path.basename(filepath)
    name, ext = os.path.splitetext(base_name)
    return name, ext

def upload_image_path(instance, filename):
    # print(instance)
    # print(filename)
    new_filename = random.randint(1, 3910209312)
    name, ext = get_filename_ext(filename)
    final_filename = '{new_filename}{ext}',format(new_filename=new_filename, ext=ext)
    return "products/{new_filename)/{final_filename}",format(
        new_filename=new_filename,
        final_filename=final_filename
        )

class ProductQuerySet(models.query.QuerySet):
    def active( self ):
        return self.filter(active = True)
    def featured( self ):
        return self.filter(featured = True, active = True)

class ProductManager(models.Manager):
    def get_queryset( self ):
        return ProductQuerySet(self.model, using = self._db)
    def all( self ):
        return self.get_queryset().active()
    def featured( self ): #Product.objects.featured
        return self.get_queryset().featured()

    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):
    title       = models.CharField(max_length=120)
    slug        = models.SlugField(blank=True, unique=True)
    description = models.TextField()
    price       = models.DecimalField(decimal_places=2, max_digits=20, 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)
    timestamp   = models.DateTimeField(auto_now_add=True)

    objects = ProductManager()

    def __str__(self):
        return self.title

    def __unicode__(self):
        return self.title
    @property
    def name(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)

1 个答案:

答案 0 :(得分:0)

指定的 namespace 包含的urls模块必须在其app_name中声明唯一的urls.py ,以允许Django to bind them together


没有urls.py的多个app_name在一个默认命名空间中。如果为一组网址声明了不同的名称空间,则它们必须具有唯一的app_name。