Please help OperationalError at /admin/

I am working on a tutorial by Corey Schafer - Python Django Tutorial: Full-Featured Web App Part 4-Admin Page. When I try to login to the admin by running 127.0.0.1:8000/admin I get this error message - OperationalError at /admin. How can I fix this?

Please post the complete error and the traceback from your runserver console.

[03/Oct/2024 09:29:48] "GET / HTTP/1.1" 200 6103
[03/Oct/2024 09:29:48] "GET /static/blogzack/main.css HTTP/1.1" 200 1373
Not Found: /favicon.ico
[03/Oct/2024 09:29:49,074] - Broken pipe from ('127.0.0.1', 60347)
Internal Server Error: /admin/
Traceback (most recent call last):
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 187, in _get_session
    return self._session_cache
           ^^^^^^^^^^^^^^^^^^^
AttributeError: 'SessionStore' object has no attribute '_session_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\sqlite3\base.py", line 329, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: django_session

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\admin\sites.py", line 259, in wrapper
    return self.admin_view(view, cacheable)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\admin\sites.py", line 228, in inner
    if not self.has_permission(request):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\admin\sites.py", line 201, in has_permission
    return request.user.is_active and request.user.is_staff
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\utils\functional.py", line 251, in inner
    self._setup()
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\utils\functional.py", line 404, in _setup
    self._wrapped = self._setupfunc()
                    ^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\auth\middleware.py", line 33, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
                                            ^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\auth\middleware.py", line 13, in get_user
    request._cached_user = auth.get_user(request)
                           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\auth\__init__.py", line 209, in get_user
    user_id = _get_user_session_key(request)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\auth\__init__.py", line 62, in _get_user_session_key
    return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
                                               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 53, in __getitem__
    return self._session[key]
           ^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\sessions\backends\base.py", line 192, in _get_session
    self._session_cache = self.load()
                          ^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\sessions\backends\db.py", line 42, in load
    s = self._get_session_from_db()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\contrib\sessions\backends\db.py", line 32, in _get_session_from_db
    return self.model.objects.get(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\query.py", line 645, in get
    num = len(clone)
          ^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\query.py", line 382, in __len__
    self._fetch_all()
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\venv\Lib\site-packages\django\db\backends\sqlite3\base.py", line 329, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: no such table: django_session
[03/Oct/2024 09:30:03] "GET /admin/ HTTP/1.1" 500 211986







This is my base.html file. When I run the development server the page opens all good and fine in the browser. However when I run 12/.0.0.1:8000/admin that is when I get the operational error.

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
{% comment %} <link rel="stylesheet" href="{% static static/css/'blogzack.css' %}">     {% endcomment %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'blogzack/main.css' %}">
{% if title %}
        <title>Blog Zack-{{ title }}</title>
    {% else %}
        <title>djangoBlog</title>
    {% endif %}
</head>
<body>
    <header class="site-header">
        <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
          <div class="container">
            <a class="navbar-brand mr-4" href="{% url 'blog-home' %}">Django Blog</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarToggle">
              <div class="navbar-nav mr-auto">
                <a class="nav-item nav-link" href="{% url 'blog-home' %}">Home</a>
                <a class="nav-item nav-link" href="{%  url 'blog-about' %}">About</a>
              </div>
              <!-- Navbar Right Side -->
              <div class="navbar-nav">
                <a class="nav-item nav-link" href="#">Login</a>
                <a class="nav-item nav-link" href="#">Register</a>
              </div>
            </div>
          </div>
        </nav>
      </header>
    {% comment %} <div class="container">
        {% block content %}{% endblock content %}
    </div> {% endcomment %}
    <main role="main" class="container">
        <div class="row">
          <div class="col-md-8">
            {% block content %}{% endblock %}
          </div>
          <div class="col-md-4">
            <div class="content-section">
              <h3>Our Sidebar</h3>
              <p class='text-muted'>You can put any information here you'd like.
                <ul class="list-group">
                  <li class="list-group-item list-group-item-light">Latest Posts</li>
                  <li class="list-group-item list-group-item-light">Announcements</li>
                  <li class="list-group-item list-group-item-light">Calendars</li>
                  <li class="list-group-item list-group-item-light">etc</li>
                </ul>
              </p>
            </div>
          </div>
        </div>
      </main>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>

</body>
</html>

Please post the INSTALLED_APPS and MIDDLEWARE sections of your settings.py file.


INSTALLED_APPS = [
    'blogzack.apps.BlogzackConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

Have you run the makemigrations and migrate commands after making your most recent changes?

What’s the output of a manage.py showmigrations command?

I ran makemigrations and got this result

WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
No changes detected in app 'blogzack'

Then you have something different in your project from what the tutorial is teaching you. (Or, it’s possible that the tutorial is too old for the current version of Django that you may be using.)

I ran showmigrations this is what I get

(venv) C:\Users\hp\Documents\Learn Python\Django\Corey Schafer\zacsProject\zacsBlog>py manage.py showmigrations
System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
admin
 [ ] 0001_initial
 [ ] 0002_logentry_remove_auto_add        
 [ ] 0003_logentry_add_action_flag_choices
auth
 [ ] 0001_initial
 [ ] 0002_alter_permission_name_max_length
 [ ] 0003_alter_user_email_max_length
 [ ] 0004_alter_user_username_opts
 [ ] 0005_alter_user_last_login_null
 [ ] 0006_require_contenttypes_0002
 [ ] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length
 [ ] 0009_alter_user_last_name_max_length
 [ ] 0010_alter_group_name_max_length
 [ ] 0011_update_proxy_permissions
 [ ] 0012_alter_user_first_name_max_length
blogzack
 (no migrations)
contenttypes
 [ ] 0001_initial
 [ ] 0002_remove_content_type_name
sessions
 [ ] 0001_initial

Yes its possible. I went to awesome django as you suggested and followed some links from the educational section. One of the tutorials there - A Complete Beginners Guide to Django by Vitor Freitas - was using regex. I had to leave it cause i didnt understand it

Maybe I missed something in the tutorial. So I am going to start all over again from the very beginning.

It looks like the migrations weren’t run. Those checkboxes should be "checked "

Eg
[x] instead of [ ]

Try running manage.py migrate

Thank you so much @Mariatta. Which checkboxes are you asking me to ‘check’. Another thing I noticed is that when I run 127.0.0.1:8000 i get the error but when i run localhost:8000 the website comes up in the browser. Also when I exit the folder, and open up another folder/virtual environment everything works just fine. 127.0.0.1:8000 works properly and 127.0.0.1:8000/admin opens up the admin dashboard/gui