Skip to content

Instantly share code, notes, and snippets.

@vucurovicmarko
Last active July 18, 2022 19:06
Show Gist options
  • Save vucurovicmarko/753be49a87cf36ca18a14b77c4ffaa17 to your computer and use it in GitHub Desktop.
Save vucurovicmarko/753be49a87cf36ca18a14b77c4ffaa17 to your computer and use it in GitHub Desktop.
Django project setup (api.nabavi.ga)

Django installation

Python 3 set up

sudo apt update
sudo apt -y upgrade
python3 -V
sudo apt install -y python3-pip
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

Setting Up a Virtual Environment

Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3

sudo apt install -y python3-venv

cd [project-directory-name]

python3 -m venv .venv
. .venv/bin/activate

To leave the environment

deactivate

Installing Django

Within virtual environment (.venv)

pip install django
django-admin --version
django-admin startproject core

Then run project on django development server

python manage.py runserver

Pip requirements

Inside virtual environment .venv

Save dependencies

pip freeze > requirements.txt

Install dependencies

pip install -r requirements.txt

Nginx

Install nginx

sudo su
apt install nginx

Create nginx config file

nano /etc/nginx/sites-available/api.nabavi.ga

paste the following:

server {
  listen 80;
  
  access_log /var/www/api.nabavi.ga/logs/nginx-access.log;
  error_log /var/www/api.nabavi.ga/logs/nginx-error.log;
  
  server_name api.nabavi.ga 127.0.0.1;
  
  location / {
    client_max_body_size 100M;
    include uwsgi_params;
    uwsgi_pass unix:/var/www/api.nabavi.ga/.venv/var/run/uwsgi.sock;
  }
  
  location /static/ {
    alias /var/www/api.nabavi.ga/static/;
  }
}

Link to sites enabled

ln -s /etc/nginx/sites-available/api.nabavi.ga /etc/nginx/sites-enabled/api.nabavi.ga
service nginx reload
nginx -t

Available options:

service nginx [start, stop, restart, reload]

Static files

Configuration

In project root create static directory

settings.py

STATIC_ROOT = BASE_DIR / 'static-files/'
STATICFILES_FINDER = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    BASE_DIR / 'static/custom'
]

Usage

In template files at top of file where static files are used

{% load static %}

CSS Link

<link rel="stylesheet" href="{% static 'cms/css/cms.css' %}">

Collecting static files

python manage.py collectstatic

DB export & import

DB export

python manage.py dumpdata > dump.json

DB import

python manage.py loaddata dump.json

PostgreSQL

Install postgreSQL

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql
sudo su postgres
psql

# list all users in current database server
\du

# show all databases in the current PostgreSQL server
\l

# closes psql
\q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment