68

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.
    }
}
starball
  • 20,030
  • 7
  • 43
  • 238
user1576866
  • 683
  • 1
  • 5
  • 7

9 Answers9

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

Philipp Schwarz
  • 18,050
  • 5
  • 32
  • 36
jdi
  • 90,542
  • 19
  • 167
  • 203
  • I can't get the python manage.py shell to work. It gives me this error: python: can't open file 'manage.py' – user1576866 Aug 05 '12 at 16:48
  • I deleted the first project from set up and got the app from the previous owner (website admin changes every year for this.) – user1576866 Aug 05 '12 at 17:00
  • 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? – jdi Aug 05 '12 at 17:49
  • 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 . – user1576866 Aug 05 '12 at 18:02
  • 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 directory – jdi Aug 05 '12 at 18:53
  • how do I change the project location? Move the folders to another location? – user1576866 Aug 05 '12 at 23:01
  • 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. – user1576866 Aug 05 '12 at 23:22
  • 1
    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 run – jdi Aug 06 '12 at 00:09
  • Ok, the chronograph files were in a different folder... manage.py runs correctly – user1576866 Aug 06 '12 at 00:28
  • the second line of the code you gave in the initial post (first one works) gives this error: unable to open database file – user1576866 Aug 06 '12 at 00:35
  • 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? – jdi Aug 06 '12 at 02:49
  • 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?: https://docs.djangoproject.com/en/dev/intro/tutorial01/ – user1576866 Aug 06 '12 at 02:57
  • Yea that first part is about creating a new project. You are probably at [this step here](https://docs.djangoproject.com/en/dev/intro/tutorial01/#database-setup) . – jdi Aug 06 '12 at 03:33
  • 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: http://olddocs.dotcloud.com/tutorials/django/#use-a-sqlite-database – jdi Aug 06 '12 at 03:37
  • ok, followed the dotcloud instructions. the settings.py , url.py, and other things I needed to edit were already correct. – user1576866 Aug 06 '12 at 04:22
  • I am assuming having run syncdb, you now have a local database that can be used. – jdi Aug 06 '12 at 04:24
  • 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) . – user1576866 Aug 06 '12 at 04:25
  • post the database configuration from your settings.py, in your question – jdi Aug 06 '12 at 04:29
  • 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. – user1576866 Aug 06 '12 at 04:31
  • ok, I posed the database config back at the top of this question – user1576866 Aug 06 '12 at 04:35
  • 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 prompt – jdi Aug 06 '12 at 04:44
  • /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? – user1576866 Aug 06 '12 at 04:50
  • 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. – jdi Aug 06 '12 at 05:07
  • oh, I see. ok, I changed the path to the place nhs.db is actually stored. – user1576866 Aug 06 '12 at 05:15
  • 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 work – user1576866 Aug 06 '12 at 05:26
  • 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. – jdi Aug 06 '12 at 05:28
  • ok, I guess I'll have do this with the previous owner. thanks for your help! – user1576866 Aug 06 '12 at 05:36
  • Thanks! All the other answers never mentioned changing the SITE_ID in settings.py – Engineer2021 Feb 03 '14 at 16:33
  • 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". – Joost Apr 06 '15 at 15:13
  • 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. – jdi Apr 06 '15 at 20:01
  • 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**: http://stackoverflow.com/a/21084927/673991 – Bob Stein Aug 19 '15 at 23:16
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).

moth
  • 345
  • 1
  • 13
11

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

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
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.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
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

Seanny123
  • 8,776
  • 13
  • 68
  • 124
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

wscourge
  • 10,657
  • 14
  • 59
  • 80
Freman Zhang
  • 491
  • 6
  • 6
1

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

Odiljon Djamalov
  • 761
  • 8
  • 11
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.

Vinay Khatri
  • 312
  • 1
  • 8
0

ensure you add site id, below is a sample:

SITE_ID = 1

Sergiu
  • 2,928
  • 3
  • 27
  • 37