So I have the following code which specifies the path of where my db file is located on my system.
It works flawlessly on the computer which I have created my django project in. However, when I move the project to a different machine, it does not find the db.sqlite3 file and instead creates a new one in the root directory of that machine, so I have to create a DB_DIR and manually specify the path and cannot use BASE_DIR for the database connection.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Does anyone have any suggestions?
Try this (Django 3):
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.