Django
Dev Extensions
pipenv shell
pipenv install flake8 --dev
pipenv install black --dev --pre
pipenv install pylint-django --dev
pipenv install autopep8 --devDjango 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.
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
.envfile at the same level assettings.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()oros.environ.get()
# settings.py
import os
SECRET_KEY = os.environ.get("SECRET_KEY")
DEBUG = "DEVELOPMENT" in os.environNote that
DEBUGwill beTrueif there's aDEVELOPMENTkey/value pair in your.envfile, regardless of its value (i.e. if it has any value it will beTrue).This means that
DEBUGwill beFalsein production, unless theDEVELOPMENTvariable 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-extensionsand add it tosettings.py:
INSTALLED_APPS = [
...
"django_extensions"
]Pip install
pygraphvizMake sure to have
graphvizinstalled in your system:
# MacOS
brew install graphviz
# Ubuntu and Debian
sudo apt-get install graphviz graphviz-devIf 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