简体   繁体   English

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

[英]Specifying a namespace in include() without providing an app_name. Giving app_name also doesn't work

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

Please Help.请帮忙。 I am getting this error :我收到此错误:

'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. '在 include() 中指定命名空间而不提供 app_name ' django.core.exceptions.ImproperlyConfigured:不支持在 include() 中指定命名空间而不提供 app_name。 Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.在包含的模块中设置 app_name 属性,或者传递一个包含模式列表和 app_name 的 2 元组。

products\\urls.py:产品\\网址.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:产品\\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)

Included urls modules with namespace specified must declare unique app_name in their urls.py to allow Django to bind them together .包含指定namespace urls 模块必须在它们的urls.py声明唯一的app_name以允许 Django将它们绑定在一起


Multiple urls.py without app_name are in one default namespace.多个没有app_name urls.py位于一个默认命名空间中。 If different namespace is declared for set of urls - they need to have unique app_name.如果为一组 url 声明了不同的命名空间 - 它们需要具有唯一的 app_name。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持在 include() 中指定命名空间而不提供 app_name - Specifying a namespace in include() without providing an app_name is not supported Django social_django&#39;在include()中指定名称空间而不提供app_name&#39; - Django social_django 'Specifying a namespace in include() without providing an app_name ' Django 2名称空间和app_name - Django 2 namespace and app_name 为什么 django-postman 不能用这个 -> url(r'^messages/', include('postman.urls', namespace='postman', app_name='postman')), - Why with django-postman that's doesn't work with this -> url(r'^messages/', include('postman.urls', namespace='postman', app_name='postman')), 在 include() 中使用命名空间时关于 app_name 的 ImproperlyConfiguredError - ImproperlyConfiguredError about app_name when using namespace in include() 如何在不提供 app_name 的情况下测试 django 应用程序 - How to test django app without giving an app_name 如何在 Django 中包含带有 app_name 的 url,而不必使用带有应用名称的反向? - How to include urls with app_name without having to use reverse with app name in Django? 在没有app_name的情况下url.py中的include关键字不起作用(Django 2.0) - Include keyword in url.py not working without app_name(Django 2.0) 未命名模块<app_name> - No module named <app_name> 与/ app_name / page或app_name / page混淆 - Confused with /app_name/page or app_name/page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM