Skip to content

Instantly share code, notes, and snippets.

@PraneethKarnena
Last active June 6, 2022 01:11
Show Gist options
  • Save PraneethKarnena/fcb19188fc6f742aa1bbe97abc59a26b to your computer and use it in GitHub Desktop.
Save PraneethKarnena/fcb19188fc6f742aa1bbe97abc59a26b to your computer and use it in GitHub Desktop.
Django Best Practices

Django Best Practices - Powered by Two Scoops of Django

  • Use Django-Cookiecutter for a base project template

  • App name should be a plural of the main model in the app, like subscriptions, payments

  • Always use BaseModel with id (UUID), created_at & updated_at. Every model sub-classes this model

  • Always use AbstractUser all the time, even though you don't need one

  • Always use multiple settings.py files, like base.py, production.py

  • Use DJANGO_SETTINGS_MODULE variable in your .env file

  • Always use a .env file for managing keys and other secret info

  • Always have multiple requirements.txt files: requirements/base.txt, requirements/dev.txt

  • Always use Django Debug Toolbar for debugging

  • Postgres is the recommended database

  • Make model choices constant

  • Instead of .get(), use get_object_or_404 [only in views]

  • Make good use of Object.DoesNotExist & Object.MultipleObjectsReturned

  • Use transaction for ATOMIC requests

  • Use Class-based views wherever possible - check Django's Generic Class-based Views

  • Implement URL namespacing

  • Use LoginRequiredMixin for granting access to authenticated users in CBV

  • Always use the name of the app as a directory for templates

  • User AUTH_USER_MODEL in settings.py

  • Other production settings available at https://gist.github.com/PraneethKarnena/d626eb91ef222d565f0f6a44cbb5cbde

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment