简体   繁体   English

Django SQLITE3 BASE_DIR 在移动机器时不起作用

[英]Django SQLITE3 BASE_DIR not working when moving machines

So I have the following code which specifies the path of where my db file is located on my system.所以我有以下代码,它指定了我的 db 文件在我的系统上的位置。

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.它在我创建 django 项目的计算机上完美运行。但是,当我将项目移动到另一台机器时,它找不到 db.sqlite3 文件,而是在该机器的根目录中创建一个新文件,所以我必须创建一个 DB_DIR 并手动指定路径,并且不能使用 BASE_DIR 进行数据库连接。

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):试试这个(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',
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM