Introduction
After doing some modification to the Django-based project or application, running a Django-based internal service in the command line end up in an error message. Those modification actually are an attempt to solve several error messages appear. The first one is an error message appear as in the article in this link. It is an article with the title of ‘How to Solve Error Message RuntimeError: Model class django.contrib.sites.models.Site doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS in Django Application’.
Unfortunately, after solving the error message as in that article, another different error message appear. It exist in the article with the title of ‘How to Solve Error Message RuntimeError: Model class django.contrib.flatpages.models.FlatPage doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS in Django Application’ in this link. The error message itself appear at the end of the log display of the Django-based internal service command execution. But actually, the error message is just pointing out something which exist as follows :
django.contrib.sites.models.Site.DoesNotExist: Site matching query does not exist
A full display of the execution of the command with the error message at the end of it exist in the following output :
(env) C:\programming\python\django\myproject>python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 20, 2021 - 13:25:37 Django version 3.2.6, using settings 'myproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Internal Server Error: /admin/login/ Traceback (most recent call last): File "C:\app\python39\lib\site-packages\django\contrib\sites\models.py", line 39, in _get_site_by_request SITE_CACHE[host] = self.get(domain__iexact=host) File "C:\app\python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\models\query.py", line 435, in get raise self.model.DoesNotExist( django.contrib.sites.models.Site.DoesNotExist: Site matching query does not exist. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\app\python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\app\python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\app\python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\contrib\admin\sites.py", line 414, in login return LoginView.as_view(**defaults)(request) File "C:\app\python39\lib\site-packages\django\views\generic\base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper return view(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\contrib\auth\views.py", line 63, in dispatch return super().dispatch(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\views\generic\base.py", line 98, in dispatch return handler(request, *args, **kwargs) File "C:\app\python39\lib\site-packages\django\views\generic\edit.py", line 133, in get return self.render_to_response(self.get_context_data()) File "C:\app\python39\lib\site-packages\django\contrib\auth\views.py", line 97, in get_context_data current_site = get_current_site(self.request) File "C:\app\python39\lib\site-packages\django\contrib\sites\shortcuts.py", line 13, in get_current_site return Site.objects.get_current(request) File "C:\app\python39\lib\site-packages\django\contrib\sites\models.py", line 60, in get_current return self._get_site_by_request(request) File "C:\app\python39\lib\site-packages\django\contrib\sites\models.py", line 45, in _get_site_by_request SITE_CACHE[domain] = self.get(domain__iexact=domain) File "C:\app\python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\models\query.py", line 435, in get raise self.model.DoesNotExist( django.contrib.sites.models.Site.DoesNotExist: Site matching query does not exist.
Solution
For the solution to the error message above, there are several steps to achieve the solution. Actually, the error itself does not appear suddenly after the execution of the Django-based internal service using ‘python manage.py runserver’. It appears after trying to access one specific URL address. So, the steps for the solution exist as follows :
-
First of all, make sure that the migration process is done. It is important to have a table with the name of ‘django_site’. For an example, it is exist as follows :
C:\>psql -Udjango_postgres -d sysapp Password for user django_postgres: psql (14.0) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. sysapp=> \dt List of relations Schema | Name | Type | Owner --------+----------------------------+-------+----------------- public | auth_group | table | django_postgres public | auth_group_permissions | table | django_postgres public | auth_permission | table | django_postgres public | auth_user | table | django_postgres public | auth_user_groups | table | django_postgres public | auth_user_user_permissions | table | django_postgres public | django_admin_log | table | django_postgres public | django_content_type | table | django_postgres public | django_flatpage | table | django_postgres public | django_flatpage_sites | table | django_postgres public | django_migrations | table | django_postgres public | django_session | table | django_postgres public | django_site | table | django_postgres (15 rows) sysapp=> select * from django_site; id | domain | name ----+-------------+------------- 1 | example.com | example.com (1 row) sysapp=>
-
After that, make sure in the table with the name of ‘django_site’ there is one row or one record available. The following is the query execution to do that :
sysapp=> select * from django_site; id | domain | name ----+-------------+------------- 1 | example.com | example.com (1 row) sysapp=>
Fortunately, there is one row representing the record. If it is empty, just insert one row or one record with the exact row or record above.
-
And then, use the id of that one row or one record as a value of parameter constant with the name of SITE_ID. For an example, if there is a row or a record with the id of 1, just use SITE_ID = 1. So, that parameter constant with its value is a solution to solve this error. Just add it into the ‘settings.py’ file exist in the folder project of the Django-based framework. Place it at the end of the content, so it will appear as in the following content :
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/static/' SITE_ID = 1
With that, the error message will actually no longer available. Just run it once more and try to access a page available in that Django-based application. If there are no more errors appear, the page will appear normally.