Software Engineering

파이썬/Django 개발환경 구축 (Ubuntu 24.04)

iseop 2025. 2. 8. 14:41

1. 필요한 패키지 식별 및 설치

  • Git
  • PIP
apt update
apt install git python3-pip -y

 

2. 최상위 프로젝트 디렉터리 생성

mkdir /mnt/sda1/project

 

3. 파이썬 가상 환경 (venv) 생성

cd /mnt/sda1/project
python -m venv 프로젝트명
cd 프로젝트명

(virtualenv등이 더 많은 기능을 제공하지만, venv로 충분하다고 판단)

 

4. venv 활성화

source bin/activate

 

5. 필요 파이썬 모듈 설치

pip install django

 

6. 장고 프로젝트 생성

django-admin startproject 장고프로젝트명

 

7. 장고 서버 접속 테스트

7.1. settings.py 수정

파일: 장고프로젝트명/settings.py

...
ALLOWED_HOSTS = ['*']
...

7.2. 서버 실행

cd 장고프로젝트명
python manage.py runserver 0.0.0.0:8000

7.3. 접속

http://서버 주소:8000

 

8. Git 리포지터리 생성 및 GitHub 연동

(깃허브 계정 - Settings - Developer Settings에 가서 액세스 토큰을 받아 온다.)

cd /var/projects/프로젝트명
git config --global user.email="이메일"
git config --global user.name="이름"
git clone <깃허브 리포지터리 주소>.git

 

9. 필요한 추가 패키지 설치 및 설정

pip install djangorestframework djangorestframework-simplejwt pymysql

 

파일: 프로젝트명/settings.py

...
INSTALLED_APPS = [
    ...
    'rest_framework',
    'rest_framework_simplejwt',
]
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DB',
        'USER': 'USER',
        'PASSWORD': '123',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'charset': 'utf8mb4',
        },
    }
}
...
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

 

파일: 앱/__init__.py

import pymysql
pymysql.install_as_MySQLdb()

 

10. 장고 앱 생성 및 간단한 REST 테스트

python manage.py startapp user_manager

10.1. 뷰 작성

파일: 앱/views.py

...
from rest_framework.decorators import api_view
from rest_framework.response import Response
...
# Create your views here.
@api_view(['GET'])
def ping(request):
    return Response({'message': 'pong'})

10.2. URL 작성

파일:  장고프로젝트명/urls.py

...
from user_manager.views import ping
...
urlpatterns = [
    ...
    path('api/ping/', ping, name='ping_pong_api'),
]

10.3. 서버 실행

python manage.py runserver 0.0.0.0:8000

10.4. 접속 및 결과 확인

http://서버주소:8000/api/ping