AttributeError: 'AppConfig' object has no attribute 'urls' when trying to runserver

AttributeError: 'AppConfig' object has no attribute 'urls' when trying to runserver

我正在使用 oscar 构建一个基于 django 的电子商务项目。我 pip 安装了 django-oscar 并按照官方文档指南设置 settings.py 和根 url.py 文件。当我尝试 运行 服务器时,出现以下错误:

AttributeError: 'AppConfig' object has no attribute 'urls'

我 运行 上 Windows 7, python 3.7.2, django 2.2.2, django-oscar 1.6.2

下面是我的 settings.py 和 url.py 文件:

Settings.py:


import os
from oscar.defaults import *

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


SECRET_KEY = '###############################'

DEBUG = True

ALLOWED_HOSTS = []


from oscar import get_core_apps
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "django.contrib.sites",
    "django.contrib.flatpages",
    "compressor",
    "widget_tweaks",
] + get_core_apps(['forked_apps.checkout'])

SITE_ID = 1
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    "oscar.apps.basket.middleware.BasketMiddleware",
    "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware",
]

AUTHETICATION_BACKENDS = (
"oscar.apps.customer.auth_backends.EmailBackend",
"django.contrib.auth.backends.ModelBackend",
)

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
ROOT_URLCONF = 'commehub_oscar.urls'

from oscar import OSCAR_MAIN_TEMPLATE_DIR

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"templates"),OSCAR_MAIN_TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.contrib.messages.context_processors.messages',

                "oscar.apps.search.context_processors.search_form",
                "oscar.apps.promotions.context_processors.promotions",
                'oscar.apps.checkout.context_processors.checkout',
                "oscar.apps.customer.notifications.context_processors.notifications",
                "oscar.core.context_processors.metadata",

            ],
        },
    },
]

WSGI_APPLICATION = 'commehub_oscar.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        'ATOMIC_REQUESTS': True,
    }
}

HAYSTACK_CONNECTIONS = {
"default":{
"ENGINE":"haystack.backends.simple_backend.SimpleEngine",
},
}


AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'
MEDIA_URL = "/media/"
STATIC_ROOT = "static"
MEDIA_ROOT = "media"

OSCAR_INITIAL_ORDER_STATUS = 'Pending'
OSCAR_INITIAL_LINE_STATUS = 'Pending'
OSCAR_ORDER_STATUS_PIPELINE = {
    'Pending': ('Being processed', 'Cancelled',),
    'Being processed': ('Processed', 'Cancelled',),
    'Cancelled': (),
}

OSCAR_DEFAULT_CURRENCY = "NGN"

url.py

from django.conf.urls import include
from django.contrib import admin
from django.urls import path
from oscar.app import application
from django.conf import settings
from django.conf.urls.static import static
from django.apps import apps

urlpatterns = [
    path("i18n/",include('django.conf.urls.i18n')),
    path('admin/', admin.site.urls),
    # path("",application.urls),
    path('', include(apps.get_app_config('oscar').urls[0])),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是完整的错误日志

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\management\commands\runserver.py", line 117,
in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\management\base.py", line 390, in check

    include_deployment_checks=include_deployment_checks,
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\management\base.py", line 377, in _run_checks

    return checks.run_checks(**kwargs)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config

    return check_resolver(resolver)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\urls\resolvers.py", line 398, in check
    for pattern in self.url_patterns:
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\utils\functional.py", line 80, in __get__

    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\user\Anaconda3\envs\mydjangoenv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\user\Desktop\python\my_django_stuffs\commehub_oscar\commehub_oscar\urls.py", line 29, in <module>
    path('', include(apps.get_app_config('oscar').urls[0])),
AttributeError: 'AppConfig' object has no attribute 'urls'


您将 Oscar 2.0 的说明与 Oscar 1.6 一起使用,这就是它对您不起作用的原因。

Oscar 2.0 中加载应用配置的方式在 Oscar 2.0 中发生了重大变化,并且不向后兼容。具体来说,Oscar 1.6 中的 URL 不是从您当前尝试加载它们的 AppConfig 对象加载的。

1.6 版的文档可在 https://django-oscar.readthedocs.io/en/releases-1.6/internals/getting_started.html 获得:

from django.conf.urls import include, url
from django.contrib import admin
from oscar.app import application

urlpatterns = [
    url(r'^i18n/', include('django.conf.urls.i18n')),

    # The Django admin is not officially supported; expect breakage.
    # Nonetheless, it's often useful for debugging.
    url(r'^admin/', include(admin.site.urls)),

    url(r'', include(application.urls)),
]