Django ImproperlyConfigured

I'm fairly new to programming and I was learning Python by following the textbook No Starch Python. There is a chapter on Django and I followed the steps of the textbook. However, when I run the program:

from django.db import models

class Topic(models.Model):
    """A topic the user is learning about"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Return a string representation of the model."""
        return self.text

I will get this error message:

Traceback (most recent call last):
  File "/Users/admin/learning_log2/learning_logs/models.py", line 4, in <module>
    class Topic(models.Model):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/conf/__init__.py", line 72, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Can anyone tell me why it is the case and what can I do? Thank you very much. P.S. I know there is already a post on this but I tried what was written without any results. Also, I'm on macOS High Sierra version 10.13.6 if this information is relevant.

I tried whatever I could find on the internet but nothing seems to work. I don't even have a fundamental understanding of the problem.

I don't even have a fundamental understanding of the problem

Since you're defining a model, Django assumes that you might want to store instances of that model in a database.

To do that, Django needs to know the connection details for your chosen database, and those details are stored in the settings.py module.

You didn't tell Django where to find that module.

You need to specify the django app name in the settings.py file before start using it.

You were created a Django Application before creating this model 'Topic' with a command

python manage.py startapp core 

you need to provide the appname (in the above case it is 'core') under settings.py file in the array like bellow

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core' #new app name

]

Back to Top