Skip to content
Austin Kong edited this page Feb 11, 2018 · 18 revisions

Getting started

Django 2.0 Documentation

sudo apt install python3-pip
sudo pip3 install Django
python3 -m django --version
python -c "import django; print(django.__path__)"

Virtual environment

pip3 install virtualenv

Create environment

virtualenv --python=`which python3` ~/.virtualenvs/djangodev

Activate venv

$ source ~/.virtualenvs/djangodev/bin/activate

Leave venv

$ deactivate

Creating a project

$ django-admin startproject mysite

Create an app

$ python manage.py startapp appname

Migrate database

$ python manage.py makemigrations polls
$ python manage.py sqlmigrate polls 0001
$ python manage.py migrate

To empty out a db

$ python manage.py flush

Dump and load database to json

For a specific app Read more

$ python manage.py dumpdata yourapp > yourapp/fixtures/app_data.json

For all registered users

$ python manage.py dumpdata auth.user > fixtures/auth_data.json

For all data

$ python manage.py dumpdata > fixtures/db.json

For all data to restore a fresh db to prevent errors Source 2

$ python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

Load data back (will look in all appropriate directories, and file type optional)

$ python manage.py loaddata db

Run a local test server at http://127.0.0.1:8000

$ python manage.py runserver

Don't run it with SSL enabled

Direct API access

$ python manage.py shell

Create admin user

$ python manage.py createsuperuser

Debugging

In mysite/settings.py set the flag DEBUG=True

Find missing static files and see where they are pulled from

$ django-admin findstatic staticfile [staticfile ...]

Split settings.py for multiple environments

Source 1 2

Specify settings file with runserver. Use . if settings is in a module/folder, else it is the file name

$ python manage.py runserver --settings=mysite.settings.local

For a permanent specification, modify the environment variable DJANGO_SETTINGS_MODULE in manage.py or wsgi.py.

Updating Django

https://docs.djangoproject.com/en/2.0/howto/upgrade-version/

pip install -U Django

Preparing for deployment

Built in depolyment check

$ python3 manage.py check --deploy

Set DEBUG=False in settings.py

DEBUG = bool( os.environ.get('DJANGO_DEBUG', True) )

Set SECRET_KEY as an environment variable

SECRET_KEY = os.environ['SECRET_KEY']

Set SECRET_KEY as a file

with open('/etc/secret_key.txt') as f:
    SECRET_KEY = f.read().strip()

Generate SECRET_KEY

Online Generator

Built-in method

python manage.py shell -c 'from django.core.management import utils; print(utils.get_random_secret_key())'

A more random(?) method source

python -c 'import random; import string; print("".join([random.SystemRandom().choice("{}{}{}".format(string.ascii_letters, string.digits, string.punctuation)) for i in range(50)]))'

Collect static files (required to run in a production environment, DEBUG=False)

$ python manage.py collectstatic

Python stuff

Model.__name__
Model.__class__.__name__
Model.__dict__
Model._meta.verbose_name