DEV Community

Younes Haddam
Younes Haddam

Posted on

Django 2.2 Cheat Sheet (2020)

Initialize a virtual environnement using venv:

yourlaptop@laptop:~/some_path/ :$ python3 -m venv env
Enter fullscreen mode Exit fullscreen mode

Activate virtual environnement :

yourlaptop@laptop:~/some_path/ :$ source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Deactivate virtual environnement:

(env) yourlaptop@laptop:~/some_path/ :$ Deactivate
Enter fullscreen mode Exit fullscreen mode

Install Django on the virtual environnement:

(env) yourlaptop@laptop:~/some_path/ :$ pip install django
Enter fullscreen mode Exit fullscreen mode

This will automatically install the latest version of django if you want to specify the version do it like this : pip install django==2.2.2

Start The Server

(env) yourlaptop@laptop:~/some_path/ :$ python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

For me i am using python3 you can use python if you want

Uninstall a library :

pip uninstall django
Enter fullscreen mode Exit fullscreen mode

Just Use pip uninstall "Library Name"

Check & Save All Installed Libraries In Your Environnement :

// This will log all libraries installed
(env) yourlaptop@laptop:~/some_path/ :$ pip freeze
// This will save them in an external text file named requirements
(env) yourlaptop@laptop:~/some_path/ :$ pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Install Libraries That you Saved :

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Create a django project :

(env) yourlaptop@laptop:~/some_path/ :$ django-admin startproject mysite
Enter fullscreen mode Exit fullscreen mode

Create a django app :

(env) yourlaptop@laptop:~/some_path/ :$ python3 manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

Make Migtations :

(env) yourlaptop@laptop:~/some_path/ :$ python3 manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

Migration :

(env) yourlaptop@laptop:~/some_path/ :$ python3 manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Create a Super user :

(env) yourlaptop@laptop:~/some_path/ :$ python3 manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Make The Secret Key In The Environnement :

First install the library:

pip install django-environ
Enter fullscreen mode Exit fullscreen mode

Create a .env file your env file must be in .gitignore
Write in .env file the your secret key like this

SECRET_KEY=yoursecretkey
Enter fullscreen mode Exit fullscreen mode

At The Top of settings.py insert The following Code

import environ
env = environ.Env()
environ.Env.read_env()
Enter fullscreen mode Exit fullscreen mode

Now You can access your Env Variable write it like this :

SECRET_KEY = env("SECRET_KEY")
Enter fullscreen mode Exit fullscreen mode

Notes :

Spaces are important in Jinja

Top comments (0)