Django the Right Way!

@_theskumar (https://saurabh-kumar.com)

Agenda

  • Basic Concepts
  • Hello world
  • FOSSEvents Project
  • Lessons Learnt

Basic Concepts

  • Tooling and setting up your development manchine
  • What/why of Django?
  • Python vs Django
  • Documentation
  • Project Generators/boilerplates (cookiecutter)
  • Testing (unittest, pytest, linting)
  • Build Tools (Makefile, Fabric)
  • Continuous Integration and Deployment

Tooling and setup your dev machine

  • Maintain dotfiles.
  • Know your editor (PyCharm, SublimeText, Atom, vim, notepad, etc.).
  • Use virtualenv per project.
  • Use shell scripting to ease your daily tasks

Editors

  • Choose them carefully and learn it well.
  • Make use of plugins: SublimeText

Virtualenv

Maintain seperation of installed libraries per project.

virtualenv venv  # copy python and it's standard libraries to `venv` folder
source venv/bin/activate  # Activate the environement

Python3 environment:

virtualenv

Protip: Setup an alias to create and activate virutalenv

alias sv=if [[ ! -d "venv" && ! -L "venv" ]] ; then; virtualenv venv; fi; source venv/bin/activate;

Further reading:

  1. virtualenv-vs-virtualenvwrapper
  2. gh:dotfiles/.aliases

What/Why of Django

  • Python Vs Django
  • Different flavours of framework (flask/django/pyramid/web.py)

Django Life-Cycle in brief

-

Linting

  • Use flake8 as soon as you can.
  • Ideally before you make a commit.
  • Run it as part of CI build process.
  • git-flake8 script, ensures you never make a mistake.
git flake8

Documentation

  • Why
  • When
  • Tools
  • How

Project Biolerplates and Generators

  • Django has it's own (django-admin.py startproject)
  • Cookiecutter
    • Uses jinja templating language for file/folder names and variable substitions.
cd code/cookeicutter_demo
pip install cookiecutter
cookiecutter .

Hello World

cd code/1_hello_world
virtualenv venv; source venv/bin/activate

Run the project:

python hello.py runserver
python hello.py test

FOSSEvents Projects

cookecutter https://github.com/Fueled/cookiecutter-django.git