-
Notifications
You must be signed in to change notification settings - Fork 0
Django
sudo apt install python3-pip
sudo pip3 install Django
python3 -m django --version
python -c "import django; print(django.__path__)"
pip3 install virtualenv
Create environment
virtualenv --python=`which python3` ~/.virtualenvs/djangodev
Activate venv
$ source ~/.virtualenvs/djangodev/bin/activate
Leave venv
$ deactivate
$ 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
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
$ python manage.py shell
$ python manage.py createsuperuser
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 ...]
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
.
https://docs.djangoproject.com/en/2.0/howto/upgrade-version/
pip install -U Django
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
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
Model.__name__
Model.__class__.__name__
Model.__dict__
Model._meta.verbose_name
This is a collection of tips and reminders for me on how to do things. What is documented here is derived from personal experience and from all over the web (some instruction or information has been copied verbatim). Original sources are provided for some of them. I have built these up over time so it has not been documented properly until now.