mkdir projectname
cd projectname
python3 -m venv myvenv
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"
pip install --upgrade pip
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
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
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.
Assuming you've got PostgreSQL already running on your machine
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
pip install psycopg2
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': '',
}
}
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Then you'll be asked a set of questions. Use strong password when asked for a password
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.