Skip to content

Latest commit

 

History

History
148 lines (123 loc) · 3.58 KB

DJANGO_cheatsheet.md

File metadata and controls

148 lines (123 loc) · 3.58 KB

Django Cheat Sheet

Setting up Django locally (Mac)

Create a project directory and cd to it

mkdir projectname
cd projectname

Create a virtual environment named "myvenv" for your django project

python3 -m venv myvenv

Start virtual environment by running

source myvenv/bin/activate

If you are using PyCharm you will also need to set up myvenv there. Simply go to Preferences > Project > Project Interpreter and in the gear menu choose "Add local"

Make sure pip is up to date

pip install --upgrade pip

Install django module

This will install the latest version of Django:

pip install django

If you want to install a specific version of Django (for example django version 1.11.0):

pip install django~=1.11.0

Install Git (if haven't already)

To install git for your system follow instructions on Git website: https://git-scm.com/. I also recommend creating a bitbucket account https://bitbucket.org/account/signup/ to create remote repositories there.

Now create a .gitignore file inside your project directory:

nano .gitignore

Once insede the nano editor type the following to exclude .idea, myvenv and pycache from being added to git:

.idea/
myvenv/
__pycache__/

Now to save .gitignore file press Control+O and to exit nano editor press Control+X

Create Django project

While inside virtual environment myvenv (check if you see (myvenv) at the beginning of a terminal line) type:

django-admin startproject projectname .

Notice the . after the statement - it tells Django to install project files inside a current directory.

Integrate with Postgres

Assuming you've got PostgreSQL already running on your machine

Prepare a database

Get into psql in terminal

psql

Create a new database my_project_database

CREATE DATABASE my_project_database;

Create a user for your new database

CREATE USER my_project_user WITH PASSWORD 'password';

Adjust user

ALTER ROLE my_project_user SET client_encoding TO 'utf8';
ALTER ROLE my_project_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE my_project_user SET timezone TO 'UTC';

Give my_project_user access to my_project_database

GRANT ALL PRIVILEGES ON DATABASE my_project_database TO my_project_user;

Exit psql

\q

Install python postgres module

pip install psycopg2

Configure django project settings to use PostgreSQL

Open settings.py in editor. Change this code

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

To this code

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'my_project_database',
        'USER': 'my_project_user',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Migrate the Database and Test your Project

python manage.py makemigrations
python manage.py migrate

Create SuperUser for your django project

python manage.py createsuperuser

Then you'll be asked a set of questions. Use strong password when asked for a password

Run a development server to test your project

python manage.py runserver 0.0.0.0:8000

Once your project is reachable through browser at http://localhost:8000 try going to http://localhost:8000/admin to see if django admin area works. You shojld be able to log in using credentials you set when you were creating a superuser.