Cheat sheet

The most common commands on one page. Activation commands are given for Windows; there is a separate line for macOS and Linux.

Physicist Enrico Fermi at a blackboard with formulas

Environment and packages

Create a separate environment for every project.

Terminal
python -m venv .venv
.venv\Scripts\activate         # Windows
source .venv/bin/activate      # macOS and Linux
pip install flask
pip freeze > requirements.txt
pip install -r requirements.txt
deactivate

Flask

A minimal app and the most useful functions.

Terminal
flask --app app run --debug
flask --app app routes
app.py
from flask import Flask, jsonify, redirect, render_template, request, url_for

app = Flask(__name__)

@app.route("/items/<int:item_id>", methods=["GET", "POST"])
def item(item_id):
    request.args.get("q")          # URL parameter ?q=
    request.form.get("title")      # form field
    request.get_json(silent=True)  # JSON body
    return render_template("item.html", item_id=item_id)

def examples():
    redirect(url_for("item", item_id=1))
    return jsonify([1, 2, 3]), 200

Django

manage.py commands and ORM basics.

Terminal
pip install django
django-admin startproject mysite .
python manage.py startapp tasks
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
python manage.py test
python manage.py shell
ORM Django
Task.objects.all()
Task.objects.filter(done=False).order_by("-id")
Task.objects.get(pk=1)
Task.objects.create(title="New")
Task.objects.filter(done=True).delete()
Task.objects.count()

SQL

The main queries. Pass values from the user only as parameters.

SQL
SELECT id, title FROM tasks WHERE done = 0 ORDER BY id DESC LIMIT 10;
INSERT INTO tasks (title, done) VALUES ('Buy bread', 0);
UPDATE tasks SET done = 1 WHERE id = 5;
DELETE FROM tasks WHERE id = 5;
SELECT done, COUNT(*) FROM tasks GROUP BY done;
SELECT t.title, u.name FROM tasks t JOIN users u ON u.id = t.user_id;

HTTP status codes

More in the REST API section.

Common status codes
CodeMeaningCodeMeaning
200Success401You need to log in
201Created403No permission
204Success, no body404Not found
301/302Redirect405Method not allowed
400Bad request500Server error

Git

More in the Git section.

Terminal
git status
git add .
git commit -m "Describe the change"
git switch -c new-feature
git switch main
git merge new-feature
git pull
git push
git log --oneline
git restore --staged app.py
git revert abc1234