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
包含的具有指定名称空间的url模块必须在url .py中声明唯一的app_name,以便Django将它们绑定在一起。 没有app_name的多个url .py位于一个默认名称空间中。如果为url集声明了不同的名称空间—它们需要有唯一的app_name。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。