Django Cheat Sheet

April 22, 2019  

My personlal cheat sheet when starting a project in Django. You can use this format or you can create a repository on GitHub and clone into it for an easier flow.

INITIALIZING PROJECT DIRECTORY, INSTALLING THE VIRTUAL ENVIRONMENT AND DJANGO:

mkdir project_name
cd project_name
virtualenv dir_name
. ./dir_name/bin/activate
pip3 install django

To test if the installation worked, inside the virtual environment command prompt, start the interactive interpreter by typing

python3

If the installation is successful, you should be able to import the django module and check the what version was installed:

Python 3.7.3 (default, Mar 27 2019, 09:23:15)
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django,get_version()
'2.2'
>>>_

Exit interpreter once done.

STARTING A PROJECT:

django-admin startproject app_name

CREATING A DATABASE:

Inside the created folder in the last step, run the following command:

python3 manage.py migrate

This creates a sqlite database and any necessary database tables.

If all goes well, this is what you’ll see:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

THE DEVELOPMENT SERVER:

To verify the migration, run:

python3 manage.py runserver

You will see the following output on the command line:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 22, 2019 - 15:24:21
Django version 2.2, using settings 'app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

That’s all folks!

REFERENCE:

BUILD YOUR FIRST WEBSITE WITH PYTHON AND DJANGO: BUILD AND DEPLOY A WEBSITE WITH PYTHON & DJANGO

Buy me a beer Buy me a beer