167๐
If you donโt have a site defined in your database and django wants to reference it, you will need to create one.
From a python manage.py shell
:
from django.contrib.sites.models import Site
new_site = Site.objects.create(domain='foo.com', name='foo.com')
print (new_site.id)
Now set that site ID in your settings.py to SITE_ID
36๐
Table django_site
must contain a row with the same value than id
(by default equals to 1
), as SITE_ID
is set to (inside your settings.py
).
- [Django]-Django: is there a way to count SQL queries from an unit test?
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg
- [Django]-Serving Media files during deployment in django 1.8
- [Django]-Django โ query filter on manytomany is empty
- [Django]-Scoped_session(sessionmaker()) or plain sessionmaker() in sqlalchemy?
7๐
I see answers to create a new site and reference id for that. But if you already have a site or somehow you deleted and created the site again from UI then the id keeps on incrementing. To solve this you can get the id as follows:
python manage.py shell
from django.contrib.sites.models import Site
print(Site.objects.get(name='example.com').id)
Get the id you get here into setting.py
. Eg.
SITE_ID = 8
Basically ID in table corresponding yo tour site and in the settings.py should match.
- [Django]-How to debug Django commands in PyCharm
- [Django]-Django โ Overriding the Model.create() method?
- [Django]-CSS styling in Django forms
5๐
I fixed it without using python manage.py
shell
At first I tried using the commands above using manage.py
shell:
from django.contrib.sites.models import Site
new_site = Site.objects.create(domain='foo.com', name='foo.com')
print(new_site.id)
But it gave out an INTEGRITY ERROR
The short answer is add SITE_ID = 1
to your settings.py
If you want to know what your site id is, then go into the actual database, I downloaded sqliteman to see what my table had. So whatever site id you have in the table is what gets assigned to SITE_ID
This is because there is a function get_current
that goes looking for SITE_ID
and it doesnโt find it in your settings.py
tables
-django_site
--Columns
---id
it should have id as 1, name as example.com
, domain as example.com
- [Django]-Django QuerySet order
- [Django]-Chained method calls indentation style in Python
- [Django]-Uninstall Django completely
1๐
Query the ID in your database django_site tables and set the right one in your Django settings.py, for example: SITE_ID = 3
- [Django]-When should I use a custom Manager versus a custom QuerySet in Django?
- [Django]-Is there a list of Pytz Timezones?
- [Django]-Django: is there a way to count SQL queries from an unit test?
- [Django]-Add a custom button to a Django application's admin page
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-How can I use Bootstrap with Django?
0๐
I got the same error while I was creating the sitemap for the project. I included this 'django.contrib.sites'
in the Installed_APP list in the setting.py
file.
By removing the 'django.contrib.sites'
from the installed_app list the error gets removed.
- [Django]-How to filter empty or NULL names in a QuerySet?
- [Django]-Django dynamic model fields
- [Django]-How to check whether the user is anonymous or not in Django?
- [Django]-WSGI vs uWSGi with Nginx
- [Django]-Django queries: how to filter objects to exclude id which is in a list?
- [Django]-Django access the length of a list within a template
0๐
Sites should be accessible via the admin panel once you add django.contrib.sites
to your appโs INSTALLED_APPS
.
Once you do this, simply access: <your_django_app_admin_url>/sites/site/
. From there, you can manage sites and see their IDs by accessing a site detail and looking at the browserโs URL, which should look similar to <your_django_app_admin_url>/sites/site/<your_site_id>/change/
.
- [Django]-Limit foreign key choices in select in an inline form in admin
- [Django]-How to filter empty or NULL names in a QuerySet?
- [Django]-Comma separated lists in django templates