Quickstart - Django REST framework

프로젝트 설정

mkdir tutorial
cd tutorial

# Create a virtual environment to isolate our package dependencies locally
python3 -m venv env
source env/bin/activate  # On Windows use `env\Scripts\activate`

# Install Django and Django REST framework into the virtual environment
pip install django
pip install djangorestframework

# Set up a new project with a single application
django-admin startproject tutorial .  # Note the trailing '.' character
cd tutorial
django-admin startapp quickstart

find .
.
./tutorial
./tutorial/asgi.py
./tutorial/__init__.py
./tutorial/settings.py
./tutorial/urls.py
./tutorial/wsgi.py
./quickstart
./quickstart/migrations
./quickstart/migrations/__init__.py
./quickstart/models.py
./quickstart/__init__.py
./quickstart/apps.py
./quickstart/admin.py
./quickstart/tests.py
./quickstart/views.py
./manage.py

python manage.py migrate
python manage.py createsuperuser --email [email protected] --username admin
password123

Rest API 테스트

python manage.py runserver

curl -H 'Accept: application/json; indent=4' -u admin:password123 <http://127.0.0.1:8000/users/>
{
    "count": 2,
    "next": null,
    "previous": null,
    "results": [
        {
            "email": "[email protected]",
            "groups": [],
            "url": "<http://127.0.0.1:8000/users/1/>",
            "username": "admin"
        },
    ]
}

Untitled

RDS 설정 및 연동

psql -h database.c0u6xj9tqa4x.ap-northeast-2.rds.amazonaws.com -p 5432 -U hyenee -d postgres
CREATE DATABASE danbi;

\c danbi;
GRANT ALL PRIVILEGES ON DATABASE danbi TO hyenee;
COMMIT;

pip install psycopg2-binary # postgreSql 드라이버 라이브러리
pip install python-decouple # config 라이브러리

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST'),
        'PORT': config('DB_PORT', default='5432'),
    }
}
DB_NAME=danbi
DB_USER=hyenee
DB_PASSWORD=skfkrh12
DB_HOST=database.c0u6xj9tqa4x.ap-northeast-2.rds.amazonaws.com
DB_PORT=5432

배포

깃 레포 생성