0

I was following the django documentation tutorial to make my first app, and then I reached the admin.py part and was trying to register my models, but then I got this error:

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.

My Folder Structure:

├───Poll
│   └───__pycache__
└───polls
    ├───migrations
    │   └───__pycache__
    └───__pycache__

Visual Representation:

enter image description here

My Code in admin.py:

from django.contrib import admin
from models import Question, Choice

admin.site.register(Question)
admin.site.register(Choice)

Settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'members.apps.MembersConfig',
]
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
HamTheMan
  • 49
  • 5
  • This post provides additional details that may help you: https://stackoverflow.com/questions/23156500/set-django-settings-module-as-an-environment-variable-in-windows-permanently It provides background information that includes what that error message is pointing you toward. This looks to me to be caused by an issue with how you installed Django and how you set the Django project up. One way to troubleshoot is to trying and start a new project with a new virtual environment to see if you can replicate the issue. Other elements include what IDE you're using. – Carewen Sep 18 '22 at 01:37
  • @HamTheMan Can you add settings.py? – Milos Bogdanovic Sep 18 '22 at 06:39

2 Answers2

0

I could see one problem i.e. import should be either from current location, using . or specify some app name.

Through current location:

from .models import Question, Choice

Through app name:

from some_app_name.models import Question, Choice
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
0

Figured it out after many frustrating hours. Basically in admin.py you register the model and instead of running the file, type py manage.py runserver.

Here's an example: Admin.py:

from django.contrib import admin
from .models import Person

admin.site.register(Person)

Instead of running the file separately, do this:

CMD:

cd yourproject
py manage.py runserver

And It works.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
HamTheMan
  • 49
  • 5