Django项目执行runserver命令报错,8000/8009端口均无法启动
Common Reasons for Django runserver Failure (Even When Switching Ports)
Hey there! Since you didn’t share the exact error output you’re getting when running python manage.py runserver 8009 or 8000, I’ll break down the most frequent culprits that cause this issue:
Hidden port occupation: Sometimes, even after switching ports, the previous runserver process might not have terminated properly (e.g., you closed the terminal without stopping it first). To check this:
- On Windows: Run
netstat -ano | findstr :8009to find the PID of the process using the port, then kill it withtaskkill /F /PID <your-pid>. - On macOS/Linux: Use
lsof -i :8009to locate the process, then terminate it withkill -9 <your-pid>.
- On Windows: Run
Misconfigured project settings:
- If your
settings.pyhas an emptyALLOWED_HOSTSlist (or doesn’t include'localhost'/'127.0.0.1'), Django will throw a security error when starting the server. Add at least one of these values to fix it. - Database configuration errors (e.g., incorrect credentials, the database server isn’t running) will prevent the server from initializing. Double-check the
DATABASESsection insettings.pyand ensure your database service is active.
- If your
Virtual environment or dependency issues:
- You might be running the command outside your activated virtual environment, meaning Django isn’t installed in your global Python context. Check if your terminal prompt shows the virtual environment name (e.g.,
(myenv)), and activate it if needed. - Incompatible Django versions: If your project was built with an older Django version but you installed a newer one, there could be breaking changes. Verify the required version in your
requirements.txtand reinstall withpip install -r requirements.txt.
- You might be running the command outside your activated virtual environment, meaning Django isn’t installed in your global Python context. Check if your terminal prompt shows the virtual environment name (e.g.,
Missing or corrupted project files:
- If
manage.pyis modified or corrupted, it can fail to launch the server. Try restoring a clean version of this file if possible. - Unapplied database migrations: If you’ve added new models but haven’t run
python manage.py migrate, the server might throw errors related to missing database tables. Run the migration command first before starting the server.
- If
Permission problems:
- While ports 8000/8009 don’t require root access, you might lack read/write permissions for project files (e.g., log files, database files). Ensure your user has the necessary permissions for the project directory.
If you can share the exact error message from your terminal, I can give you a more targeted solution!
内容的提问来源于stack exchange,提问作者sarvit sarvit
相关产品推荐
相关产品推荐

