Chapter 18 · 10 min read
Troubleshooting Guide
Troubleshooting Guide
Every Django developer meets the same errors. This chapter lists the most common ones with their meaning and fix. Read error messages from the bottom up: the last lines name the real problem, and the lines above show where it happened.
Environment and setup
| Symptom | Likely cause | Fix |
|---|---|---|
No module named django | The virtual environment is not active | Run the activate command again |
| pip finds no matching distribution for Django 6.1 | Python is older than 3.12 | Install Python 3.12 or newer, or use Django 5.2 LTS |
can't open file 'manage.py' | You are in the wrong folder | cd to the folder containing manage.py |
django-admin not found | The environment is not active | Activate it, or run python -m django startproject config . |
PowerShell blocks activate | Execution policy | Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, or use Command Prompt |
Error: That port is already in use | Another server is running | Stop it, or use runserver 8080 |
URLs and views
| Symptom | Likely cause | Fix |
|---|---|---|
| 404 page listing tried URLs | No pattern matches | Compare the address with the list; check include() and slashes |
The view didn't return an HttpResponse object | Missing return | Return HttpResponse or render(...) |
NoReverseMatch | URL name does not exist or has the wrong arguments | Check name=, app_name and the {% url %} call |
ImportError in urls.py | Missing import | Import include, path and your views |
TypeError: view() got an unexpected keyword argument | Converter name differs from the parameter name | Make them identical |
Templates and static files
| Symptom | Likely cause | Fix |
|---|---|---|
TemplateDoesNotExist | Wrong folder, or the app is not installed | Use blog/templates/blog/name.html and check INSTALLED_APPS |
Invalid block tag ... 'static' | Missing {% load static %} | Add it at the top of the template |
| CSS does not change | Browser cache | Hard-refresh with Ctrl+Shift+R |
| CSS missing in production | collectstatic not run or not served | Run it and serve STATIC_ROOT |
| Variable prints nothing | Wrong name in the context | Check the dictionary key in the view |
Models, migrations and the ORM
| Symptom | Likely cause | Fix |
|---|---|---|
no such table | Migrations not applied | python manage.py migrate |
no such column | Model changed, no migration | makemigrations, then migrate |
No changes detected | App not in INSTALLED_APPS, or no model change | Register the app |
DoesNotExist | get() found nothing | Use filter().first() or get_object_or_404() |
MultipleObjectsReturned | get() found several rows | Use a more specific lookup or filter() |
NOT NULL constraint failed | A required value is missing | Set the field, such as the author, before saving |
IntegrityError for unique fields | Duplicate value | Choose a different value |
Forms and security
| Symptom | Likely cause | Fix |
|---|---|---|
403 CSRF verification failed | Missing {% csrf_token %} | Add it inside the POST form |
| Form data appears in the URL | Form has no method="post" | Add it |
| Form saves twice on refresh | No redirect after POST | Redirect after success |
| Logout gives 405 | Using a link | Use a POST form with {% csrf_token %} |
After login, 404 at /accounts/profile/ | Missing LOGIN_REDIRECT_URL | Set it in settings |
403 Forbidden on edit | UserPassesTestMixin rejected the user | Log in as the author |
| Every request returns 400 in production | ALLOWED_HOSTS is empty | Set it to your domain |
A debugging routine
- Read the last line of the error, then the file and line number just above it.
- Reproduce the problem with the smallest change you can.
- Use
print()in a view, or inspect objects inpython manage.py shell. - Run
python manage.py checkfor configuration problems. - Run
python manage.py testto see whether something else broke. - Search the exact error message, and check the official docs for your version.
- Ask on the Django Forum with the full error and the code involved.
Starting fresh safely
In development you can delete db.sqlite3 and the numbered files in blog/migrations/ (keep __init__.py), then run makemigrations and migrate. You lose all data in that database. Never do this in production.
Official links
- Django Forum: https://forum.djangoproject.com/
- Settings reference: https://docs.djangoproject.com/en/6.1/ref/settings/
