My Brain
My Brain

Django

Dev Extensions

pipenv shell
pipenv install flake8 --dev
pipenv install black --dev --pre
pipenv install pylint-django --dev
pipenv install autopep8 --dev

Django reminders

When importing apps, if using signals that modify a Django class, import the full path to the configuration class or the app will use the base one, never registering the signals.

Stack Overflow cue

In config.py:

INSTALLED_APPS = [
-   'users',
+   'users.apps.UsersConfig',
]

See apps.py for the configuration class name:

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'users'

    def ready(self):
        import users.signals

(See also this link).


Manifest error when serving a React build

When this error occurs

manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.

Add the url to the manifest in the base urls.py file, such as:

  from django.views.generic import TemplateView

  [...]
  urlpatterns = [
    path('manifest.json', TemplateView.as_view(template_name='manifest.json', content_type='application/json'))
    [...]
  ]
  • About how to display the favicon, the author of the tutorial responds:

For favicon this is something I typically do on the backend server when the app is in production. So if you have something like an Nginx server running, then you can add a location:

 location = /favicon.ico {
     alias /var/www/mySite/img/favicon.ico;
 }

Then your alias would be the path where you put the favicon, so the Nginx server is basically in front of your wsgi server and you can use it to show the favicon

Django Tutorials

Run Django in Docker container

  • Create Django app and install dependencies
  • Pip install Gunicorn
  • Create requirements.txt
  • Install VSCode extension Remote Explorer
  • From the command palette, run Remote-Containers: Add Development Container Configuration Files
  • Select Python3
  • Uncomment the following line from .devcontainer/devcontainer.json
{
  "postCreateCommand": "pip install --user -r requirements.txt"
}

(Currently trying to get pipenv to work)

{
  "postCreateCommand": "pip install pipenv && pipenv shell && pipenv install --user -r requirements.txt"
}
  • Choose Remote-Container: Reopen in Container
  • Once open in the container, select the appropriate Python interpreter.
  • In the debugger, select a python/django configuration file.
  • Run debugger

Environment Variables

There are several efficient and flexible methods to deal with environment variables in Django, amongst them using python-dotenv and django-environ

While both achieve the same, I recommend python-dotenv because it's a better maintained project with regular updates, and so I will only focus on it.

How to set up

  • Install the package with your preferred package manager (pip, pipenv, poetry)
pip install python-dotenv
  • Create a .env file at the same level as settings.py
  • Add your variables in that file as needed, with this format (no quotes required):
SECRET_KEY=yoursecretkey
DEVELOPMENT=development
ANY_OTHER_VARIABLE=anyothervalue
  • Add the following to the top of your settings.py
# settings.py
from dotenv import load_dotenv
load_dotenv()
  • Call your variables either with os.getenv() or os.environ.get()
# settings.py
import os
SECRET_KEY = os.environ.get("SECRET_KEY")
DEBUG = "DEVELOPMENT" in os.environ

Note that DEBUG will be True if there's a DEVELOPMENT key/value pair in your .env file, regardless of its value (i.e. if it has any value it will be True).

This means that DEBUG will be False in production, unless the DEVELOPMENT variable is added to the production host's environment variables.

Don't forget to add .env to your .gitignore and never track it with version control!

Create a Graph of your Models

  • Pip install django-extensions and add it to settings.py:
INSTALLED_APPS = [
  ...
  "django_extensions"
]
  • Pip install pygraphviz

  • Make sure to have graphviz installed in your system:

# MacOS
brew install graphviz

# Ubuntu and Debian
sudo apt-get install graphviz graphviz-dev
  • If pip is unable to find the global graphviz, pip install it.

  • Add this variable to settings.py:

GRAPH_MODELS = {
  'all_applications': False, # set to True if you want all models included
  'group_models': True, # if you want to group models with a box around each group
  'app_labels': ["app1", "app2", "app3"], # add here the apps you want in the graph
}
  • Run to generate graph:
./manage.py graph_models -o models.png
  • See additional options here

Backlinks