Unable to install Django-Debug-Toolbar

Hi there, I’m following the instruction in Mosh video about installing django-debug-toolbar on video 12 of Fundamentals in the ultimate django series and getting this error when loading http://127.0.0.1:8000/playground/hello/ after following Mosh’s instruction on installing the toolbar:

RuntimeError at /playground/hello/

Model class django.contrib.sessions.models.Session doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.

Request Method: GET
Request URL: http://127.0.0.1:8000/playground/hello/
Django Version: 3.2.5
Exception Type: RuntimeError
Exception Value: Model class django.contrib.sessions.models.Session doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
Exception Location: /Users/enricobonardi/.virtualenvs/storefront-cv1BudZW/lib/python3.9/site-packages/django/db/models/base.py, line 113, in new
Python Executable: /Users/enricobonardi/.virtualenvs/storefront-cv1BudZW/bin/python3
Python Version: 3.9.5
Python Path: [’/Users/enricobonardi/CODING/ULTIMATE DJANGO PT.1/storefront’, ‘/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip’, ‘/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9’, ‘/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload’, ‘/Users/enricobonardi/.virtualenvs/storefront-cv1BudZW/lib/python3.9/site-packages’]

any ideas where’s my mistake?

Solved… in a previous video Mosh talk about removing ‘django.contrib.sessions’ from INSTALLED_APPS because is legacy… but adding again solved my problem.

Hi
I am running thru the same issue , and removing ‘django.contrib.sessions’ didn’t solve the problem
it look likes the import is not working

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.2.5
Python Version: 3.9.6
Installed Applications:
[‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘playground’,
‘debug_toolbar’]
Installed Middleware:
[‘debug_toolbar.middleware.DebugToolbarMiddleware’,
‘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’]

Traceback (most recent call last):
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\base.py”, line 233, in _get_session
return self._session_cache

During handling of the above exception (‘SessionStore’ object has no attribute ‘_session_cache’), another exception occurred:
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 32, in _get_session_from_db
return self.model.objects.get(
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\utils\functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 28, in model
return self.get_model_class()
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 23, in get_model_class
from django.contrib.sessions.models import Session
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\models.py”, line 10, in
class Session(AbstractBaseSession):
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\db\models\base.py”, line 113, in new
raise RuntimeError(

During handling of the above exception (Model class django.contrib.sessions.models.Session doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.), another exception occurred:
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\core\handlers\exception.py”, line 47, in inner
response = get_response(request)
File “c:\users\the_m.virtualenvs\storefront-ynfqwea1\src\django-debug-toolbar\debug_toolbar\middleware.py”, line 67, in call
panel.generate_stats(request, response)
File “c:\users\the_m.virtualenvs\storefront-ynfqwea1\src\django-debug-toolbar\debug_toolbar\panels\request.py”, line 66, in generate_stats
for k in sorted(request.session.keys())
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\base.py”, line 169, in keys
return self._session.keys()
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\base.py”, line 238, in _get_session
self._session_cache = self.load()
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 43, in load
s = self._get_session_from_db()
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 36, in _get_session_from_db
except (self.model.DoesNotExist, SuspiciousOperation) as e:
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\utils\functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 28, in model
return self.get_model_class()
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\backends\db.py”, line 23, in get_model_class
from django.contrib.sessions.models import Session
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\contrib\sessions\models.py”, line 10, in
class Session(AbstractBaseSession):
File “C:\Users\the_m.virtualenvs\storefront-ynfQWea1\lib\site-packages\django\db\models\base.py”, line 113, in new
raise RuntimeError(

Exception Type: RuntimeError at /
Exception Value: Model class django.contrib.sessions.models.Session doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.

help please !!!

I solved my problem by putting back ‘django.contrib.sessions’ in INSTALLED_APPS (removed as stated by Mosh in a previous video) and everything works after that. It seems you get the same error so check the presence of ‘django.contrib.sessions’ in settings.py. If you get the error you can try the solution found here:

https://stackoverflow.com/questions/35388637/runtimeerror-model-class-django-contrib-sites-models-site-doesnt-declare-an-ex

The same happened to me. I fixed the : removing ‘django.contrib.sessions’.
I continued to have an error message.

I run: python manage.py migrate
and it is working now.

Hope this works for you.

6 Likes

I can confirm that putting back the one line of code and running migrate solved the issue for me as well.

Hello everyone, adding django.contrib.sessions, didn’t solve it for me
Actually when I add it, I get
TypeError: 'module' object is not callable

It worked for me. Thanks.
Mosh should update that video where he tells us to remove that culprit line. Or explain to us what to do if we keep it removed.

The error message (shown on top of this thread) is actually telling us that it is missing that session app and because of that its related model class can’t do its job (declare):

“Model class django.contrib.sessions.models.Session doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.”

That tells me in a vague way what happened but it does not explain it to me.
Class are basically objects, so declare is probably a method of that session class. It’s worth checking the models.py file to understand it further.