This contains a collection of common errors people make when coding Django apps, and how to fix them.
- Always do one thing at a time and test it. Make a single change, test it for errors, before moving on to the next thing.
- When trying to fix an error, only change one thing at a time, If the error message does not change, undo that change and try something else.
- If you discover you haven't saved a file, and that's causing an error, save the file and try again and pay attention to any new errors. The original error isn't worth worrying about until you have saved everything
- Get in the habit of checking the tab in VSCode for the circle that indicates you haven't saved a file.
- Make sure you read the project instructions carefully! Skipping steps can cause errors.
- Don't forget to activate your virtual environment before running any django commands!
- If your browser doesn't load your site, make sure your django server is running, and doesn't have any errors happening when it starts.
- When you get an error, make sure you try to identify the file and line number of the error. Note: Some errors may point to files inside the Django codebase. In these cases, the line number and file won't be as useful.
- If you don't see your changes reflected on the website, make sure you saved your file!
- I get a
module not found
error or anot defined
error, check to make sure you've got your import line correct.
Often naming things properly in Django depends on choosing the correct Plural or Singular form of words. Here's some general rules to live by if you want to avoid confusion:
- Name Django apps plural
- Name Django Models singular
- Name the related name plural in a One-to-Many or Many-to-Many relationship
- Name the attribute of a One-to-Many relationship singular.
- Name the attribute of a Many-to-Many relationship plural
- When setting up a Django project, don't forget to add the
.
at the end of thedjango-admin startproject
command. - When adding a django app, make sure you remember to add it to the
INSTALLED_APPS
list insettings.py
- Don't proceed to the next step until you can run the current step without errors.
- Make sure you add all the fields you need and save your model.py file before
running
makemigrations
- Whenever you change a model, run
makemigrations
followed bymigrate
- When adding a ForeignKey or ManyToManyField, make sure you put the name of the Model as a string as the first argument to the ForeignKey function call.
- When adding a ForeignKey or ManyToManyField from another Django app, make
sure you prefix the Model name with the app name, and separate the two with
a
.
- Model names should be capitalized nouns and should be singular.
makemigrations
says it detects no changes to my model. Make sure you saved your models.py file.
This means Django can't find a column in the database. This usually happens
before you added a column to the model but didn't run makemigrations
and/or
migrate
before trying to use the site.
See the error above, it's usually the same reason, except Django can't find the table in the database.
Make sure the path to the template file django is trying to load matches the actual file in your templates folder. This error will usually display the file it's trying to load.
Usually this is because you used the {% url %}
tag in a template, and you passed it something invalid. It can also happen if you use the reverse
or reverse_lazy
function and pass it something invalid.
When using a Django template filter, you may get this error if you put a space between the filter name and it's arguments.
{{ description | default: "Default Description" }}
^
The extra space between the filter (default
) and the
string causes this error.
When you reference a variable that doesn't exist in a template, Django does not give you an error like python would. Instead it just prints an empty string.
As you can imagine, this isn't useful for seeing what you've done incorrectly.
What you need to do is look at the values in the context dictionary to make sure the variables you've used in your template are what you expect them to be.
If you are using a class based view, you can add a method like this to your view class to log the context dictionary to the terminal.
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
print(context)
return context
Optionally you can install the setup the Django Debug Toolbar, which has an option to view the context dictionary for a page.
- Stop your server if it's running
- Delete you database file
db.sqlite3
- Delete the migration files you are having issues with
- Run
makemigrations
again - Run
migrate
again - Run
createsuperuser
again