Site matching query does not exist
Asked Answered
W

10

70

The site was working very well until I clicked "log out" on my app. After that, the website would give me this error: DoesNotExist at /login/ Site matching query does not exist.

I searched everywhere and the only solution I get relates to setting up the site framework, SITE_ID, etc. I think those items on my computer are fine, but I can't find a walkthrough/guide to help me check on them.

What's the problem and how can it be fixed?

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/home/dotcloud/nhs.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
Wheatear answered 5/8, 2012 at 4:51 Comment(2)
Are you able to login to the admin site ? it seems that your django_site table is empty. you need at least one entry over thereFoxworth
No, I can't log in to the admin site. It gives me the same error at /admin/Wheatear
P
169

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

Pistoleer answered 5/8, 2012 at 5:45 Comment(31)
I can't get the python manage.py shell to work. It gives me this error: python: can't open file 'manage.py'Wheatear
I deleted the first project from set up and got the app from the previous owner (website admin changes every year for this.)Wheatear
Well this sounds like a bigger problem than just missing a database entry. If you cant even run manage.py, then you can't even start the dev server to test locally. Are you sure you are running this within the project location that actually contains the manage.py script?Pistoleer
I'm probably not running within the project location. It's currently running in C:\Users\MyName>. But the manage.py file for this app is in C:\Users\MyName\Documents\Websites\NHS .Wheatear
Well that would be it then. You need to be in your project location. The shell is telling you there is no manage.py in your current directoryPistoleer
how do I change the project location? Move the folders to another location?Wheatear
oh that (like I said I'm new to this). I tried python manage.py shell in the right location and it tells me: Error: No module named chronograph.Wheatear
Then the project has extra library dependencies that you dont have installed on your machine yet. You will need to install all 3rd party requirements that are being used. But at least you got manage.py to runPistoleer
Ok, the chronograph files were in a different folder... manage.py runs correctlyWheatear
the second line of the code you gave in the initial post (first one works) gives this error: unable to open database fileWheatear
You dont have your project even configured, with your database. What database is it using? Did you take the steps to actually set it up? Did you run syncdb ? It sounds to me like you just copied this project over and started trying to use it?Pistoleer
yes, I just copied the project over (I had a tech guy overseeing stuff. guess he forgot about configuring?) it's using dotcloud. is this the set up?: docs.djangoproject.com/en/dev/intro/tutorial01Wheatear
Yea that first part is about creating a new project. You are probably at this step here .Pistoleer
Actually, i see there is a dotcloud-specific doc. It says you are using sqlite3 for your database? So I assume that this project is properly deployed to dotcloud, but you are trying to run it locally as a dev server, and your sqlite does not exist locally: olddocs.dotcloud.com/tutorials/django/#use-a-sqlite-databasePistoleer
ok, followed the dotcloud instructions. the settings.py , url.py, and other things I needed to edit were already correct.Wheatear
I am assuming having run syncdb, you now have a local database that can be used.Pistoleer
retried the original code. same error. it stops at a file in Django, db, backends, sqlite3, base,py at this line: self.connection = Database.connect(**kwargs) .Wheatear
post the database configuration from your settings.py, in your questionPistoleer
syncdb ran in cygdriver (I think it's a Linux simulator or something along those lines since I'm on Windows) but it still has the database error in the C:\ run.Wheatear
ok, I posed the database config back at the top of this questionWheatear
You are using the same settings as your server (as opposed to a local version). Are you sure /home/dotcloud/nhs.db is accessible locally as a valid path? That is the physical location it wants to create the sqlite3 database. That is a UNIX path, yet you are running this under a `C:` command promptPistoleer
/home/dotcloud/nhs.db is not in the sqlite3 folder or anywhere in the python27 folder (is folder the same as database?) should I have downloaded it?Wheatear
No. It would get created by the syncdb process if you did not already have an existing database. My point is that you would need to change that to a valid path for your local system.Pistoleer
oh, I see. ok, I changed the path to the place nhs.db is actually stored.Wheatear
when I do dotcloud run etc. syncdb it has an error: can't open file 'current/nhs/manage.py' I've tried to fix it (copy the manage.py and post it one folder up, move directories with cd) but nothing seems to workWheatear
Unfortunately, I think there are too many factors here. Its a copied project, and I have major doubts that your environment is correct, or the paths. its just not currently configured for your local environment.Pistoleer
ok, I guess I'll have do this with the previous owner. thanks for your help!Wheatear
Thanks! All the other answers never mentioned changing the SITE_ID in settings.pyPeek
Is there not a way to call Site.objects.create other than directly from the management shell? It seems odd that this is not documented clearly, yet it is vital to setting up the "sites framework".Jacks
I don't know what caused it to happen. It is supposed to happen automatically when creating a new project. This was also 2.5 years ago. so maybe it has since been resolved.Pistoleer
Warning, if you already defined your site in /admin/sites/ and don't want to create a new one, here's how to get an existing SITE_ID: https://mcmap.net/q/281041/-finding-the-site_id-of-a-site-in-django-adminHousecarl
P
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).

Pass answered 17/1, 2013 at 21:49 Comment(1)
Brilliant Thanks BroMaxfield
L
11

Add SITE_ID = 1 to settings.py in your django project.

Lesslie answered 1/2, 2019 at 11:22 Comment(3)
You should explain in more detail including whyExcitant
No, in my case the opposite, removing SITE_ID = 1 from the config did work. What the hell is going onDiscontinuation
literally i spent the full day trying to solve this, reinstalling stuff, playing with migrations and so on.. in the end putting this 1 solved it all.... updates are a nightmare sometimesRetaliate
J
8

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.

Jota answered 6/2, 2018 at 7:23 Comment(0)
M
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

Microscopy answered 6/12, 2017 at 11:31 Comment(0)
B
1

enter image description here

Query the ID in your database django_site tables and set the right one in your Django settings.py, for example: SITE_ID = 3

Backbreaker answered 1/4, 2019 at 8:55 Comment(0)
A
1

it seems you fotgot to add SITE_ID=1 in settings.py

Anhwei answered 8/11, 2020 at 5:20 Comment(0)
O
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.

Otherness answered 2/11, 2022 at 7:27 Comment(0)
E
0

ensure you add site id, below is a sample:

SITE_ID = 1

Evocation answered 15/11, 2022 at 12:32 Comment(0)
L
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/.

Logger answered 8/10, 2023 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.