开发者社区> 问答> 正文

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

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),
]

请帮助。我得到这个错误: 产品\ 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'),
    ]

产品\ 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)

问题来源StackOverflow 地址:/questions/59381289/specifying-a-namespace-in-include-without-providing-an-app-name-giving-app-na

展开
收起
kun坤 2019-12-28 13:52:36 471 0
1 条回答
写回答
取消 提交回答
  • 包含的具有指定名称空间的url模块必须在url .py中声明唯一的app_name,以便Django将它们绑定在一起。 没有app_name的多个url .py位于一个默认名称空间中。如果为url集声明了不同的名称空间—它们需要有唯一的app_name。

    2019-12-28 13:52:43
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
女性移动App安全攻防战 立即下载
汇聚云计算的生态核能——云市场,云上APP Store 立即下载
千万级用户直播App——服务端架构设计和思考 立即下载

相关实验场景

更多