0

I am trying to setup a virtual environment to test a website I'm working on that is running on django. When I created the project, it didn't include a settings.py file which I got a lot of errors about before and added one using a base example and changed some things. I then added django.contrib.sites to my settings.py file which others said helped, but didn't fix it.

I ran the following commands pip install -r req.txt and when I try to run python manage.py migrate I get the error

    >> 
Traceback (most recent call last):
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\manage.py", line 22, in <module>
    main()
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\migrate.py", line 100, in handle
    self.check(databases=[database])
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
           ^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 494, in check
    for pattern in self.url_patterns:
                   ^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\demo\urls.py", line 2, in <module>
    from apps.core.views import *
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\apps\core\views.py", line 16, in <module>
    from .forms import OrderForm
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\apps\core\forms.py", line 3, in <module>
    from .models import Order
  File "C:\Users\osnys\OneDrive\Skrivebord\nordicusv\apps\core\models.py", line 8, in <module>
    class Order(models.Model):
  File "C:\Users\osnys\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 134, in __new__
    raise RuntimeError(
RuntimeError: Model class apps.core.models.Order doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

My settings.py file looks as follows:

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = 'ga=-j1+6uv^bvcr1da*!a9z)w3_!+u3c!2c*-fwct8e0@bvzl%'

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    # Add your custom apps here
]

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.XContentOptionsMiddleware',
]

ROOT_URLCONF = 'demo.urls'

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

WSGI_APPLICATION = 'your_project.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

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/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Any help on what could fix it?

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
oNysten
  • 11
  • 3
  • 1
    Did you try adding `nordicusv.apps.core` or `apps.core` to your `INSTALLED_APPS` tuple? – MatsLindh May 03 '23 at 13:29
  • that gave me the error "ModuleNotFoundError: No module named 'nordicusv'" – oNysten May 03 '23 at 13:31
  • possible duplicate: https://stackoverflow.com/questions/71987652/runtimeerror-model-doesnt-declare-an-explicit-app-label-and-isnt-in-an-applic Check that nordicusv is on your path. Also see this section of the Django docs: https://docs.djangoproject.com/en/4.2/ref/applications/#configuring-applications – Sarah Messer May 03 '23 at 13:34
  • Try adding **core** to your INSTALLED_APPS – rahul.m May 03 '23 at 13:40

1 Answers1

0

You have not added the application in your INSTALLED_APPS in settings. In Django everything breaks down to applications. They are supposed to be reusable and thus decoupled from other apps. To use the apps, first you have to declare that which apps you want to use in settings, that is why the INSTALLED_APPS exists. First, Django reads all the configs you have put in settings, and then it will startup each app you mentioned in INSTALLED_APPS. So you can create or use an already existing app by just adding it to INSTALLED_APPS in settings. Each app has a name which is declared in apps.py. For your case it is probably something like apps.core.

So you must do something like this:

# rest of the code
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'apps.core',
]
# rest of the code
Sajad Rezvani
  • 366
  • 2
  • 14