Django Admin登录时出现'unable to open database file'错误的解决方法
Got it, let's work through this error you're hitting. Since your code ran fine before, this is almost always a file permission, missing file, or path issue—let's break down the fixes step by step:
1. Check if the SQLite database file exists
First, confirm that db.sqlite3 is actually in your project's root directory (the same folder that contains your DX4C_Django project folder, where settings.py lives).
- On Linux/macOS, run this in your terminal from the project root:
ls -l db.sqlite3 - On Windows, use File Explorer to navigate to the project root and look for the file.
If the file is missing:
- If you have a backup, restore it to the correct location.
- If not, run
python manage.py migrateto create a new empty database (note: this will erase any existing data if the file was corrupted and lost).
2. Fix file and directory permissions
SQLite needs read/write access to both the db.sqlite3 file and the directory it's in. Here's how to fix this:
Linux/macOS
Run these commands (replace /path/to/your/project with your actual project root path):
# Give read/write permissions to the database file sudo chmod 664 /path/to/your/project/db.sqlite3 # Ensure the project directory has proper access sudo chmod 755 /path/to/your/project # Make sure the server user owns the file (if running on a server) sudo chown your-username:your-group /path/to/your/project/db.sqlite3
Windows
- Right-click the
db.sqlite3file → Properties - Go to the Security tab
- Select your user account and make sure you have Read & Write permissions enabled.
3. Verify the database path in settings.py
Your current config uses str(BASE_DIR / "db.sqlite3"), which is correct for Django 3.2, but let's double-check the path is accurate:
- Run this command from your project root:
python -c "from pathlib import Path; print(Path(__file__).resolve().parent.parent)"
This will print the exact path BASE_DIR points to. Confirm that db.sqlite3 is in that folder. If not, update the NAME value in DATABASES to use the full absolute path to your database file, like:
'NAME': '/home/your-user/projects/DX4C_Django/db.sqlite3',
4. Check for locked database files
Sometimes leftover processes (like a previous Django server instance that didn't shut down properly, or a SQLite viewer tool) can lock the database file.
- Close all terminal windows running Django, any database tools, and restart your server with
python manage.py runserver.
5. Test the database connection directly
Use Django's shell to test if you can connect to the database, which might give more detailed error info:
- Run:
python manage.py shell - In the shell, enter:
from django.db import connection connection.ensure_connection()
If there's an error here, the traceback will tell you exactly what's wrong (like permission denied, file not found, etc.).
If none of these steps work, your db.sqlite3 file might be corrupted. In that case, restore from a backup or create a new database with python manage.py migrate and re-import any necessary data.
内容的提问来源于stack exchange,提问作者Rika Das

