Skip to content

Instantly share code, notes, and snippets.

@bartdorsey
Last active November 21, 2023 13:54
Show Gist options
  • Save bartdorsey/4a1db19829e9f308dd91554db8039abe to your computer and use it in GitHub Desktop.
Save bartdorsey/4a1db19829e9f308dd91554db8039abe to your computer and use it in GitHub Desktop.
Common Django Pitfalls

Common Django Pitfalls

This contains a collection of common errors people make when coding Django apps, and how to fix them.

Important Strategies for preventing pitfalls

  1. 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.
  2. 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.
  3. 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
  4. Get in the habit of checking the tab in VSCode for the circle that indicates you haven't saved a file.
  5. Make sure you read the project instructions carefully! Skipping steps can cause errors.

Common Pitfalls

  1. Don't forget to activate your virtual environment before running any django commands!
  2. 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.
  3. 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.
  4. If you don't see your changes reflected on the website, make sure you saved your file!
  5. I get a module not found error or a not defined error, check to make sure you've got your import line correct.

Pluralization

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:

  1. Name Django apps plural
  2. Name Django Models singular
  3. Name the related name plural in a One-to-Many or Many-to-Many relationship
  4. Name the attribute of a One-to-Many relationship singular.
  5. Name the attribute of a Many-to-Many relationship plural

Setting up Django Pitfalls

  1. When setting up a Django project, don't forget to add the . at the end of the django-admin startproject command.
  2. When adding a django app, make sure you remember to add it to the INSTALLED_APPS list in settings.py
  3. Don't proceed to the next step until you can run the current step without errors.

Model Pitfalls

  1. Make sure you add all the fields you need and save your model.py file before running makemigrations
  2. Whenever you change a model, run makemigrations followed by migrate
  3. 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.
  4. 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 .
  5. Model names should be capitalized nouns and should be singular.

Migration Pitfalls

  1. makemigrations says it detects no changes to my model. Make sure you saved your models.py file.

Common Errors

Help, I got an column not found error!

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.

Help, I got a table not found error!

See the error above, it's usually the same reason, except Django can't find the table in the database.

Help, I got a template not found error!

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.

Help, I got an error about a NoReverseMatch

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.

Help, I got an error about a template filter requiring 2 arguments but 1 given!

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.

Help! My template isn't rendering what I expect, but there are no errors!

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.

Help! I screwed up my migrations and models and I just want to start over!

  1. Stop your server if it's running
  2. Delete you database file db.sqlite3
  3. Delete the migration files you are having issues with
  4. Run makemigrations again
  5. Run migrate again
  6. Run createsuperuser again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment