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

Sentry image

Make it make sense

Only get the information you need to fix your code that’s broken with Sentry.

Start debugging →

Top comments (0)

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay