me

[python/setting] 파이썬 – Django 설치 + 셋팅
2023.11.20 / , ,

 Django설치 다운로드

Django 설치 용 다운로드 : https://www.djangoproject.com/download/


가상 환경에 Django 설치

pip install django==”버전입력”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install Django==4.2.7
pip list - 장고 설치된 건지 확인 해주기~
pip install Django==4.2.7 pip list - 장고 설치된 건지 확인 해주기~
pip install Django==4.2.7

pip list - 장고 설치된 건지 확인 해주기~
Django 설치 리스트

프로젝트 생성

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
django-admin startproject back .
django-admin startproject back .
django-admin startproject back .
Django 설치 - 프로젝트 생성

Maria DB 변경 설치

기본적으로 Django에서는 sqlite3를 쓰고 있음

Maria DB 로 변경 해주기

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install mysqlclient
pip install mysqlclient
pip install mysqlclient
Django 설치 - mysqlclient 설치

MariaDB 설치

MariaDB : https://mariadb.org/download/?t=mariadb&p=mariadb&r=11.3.2&os=windows&cpu=x86_64&pkg=msi&mirror=blendbyte

기존 3306 포트는 이미 사용중이라 3308로 임시 변경 설치


DB 생성

윈도우 시작 > MariaDB 11.1 (x64) > MySQL Client (MariaDB 11.1 (x64)) 파일 선택

창 뜨면 root 비밀번호 넣고

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
show databases;
create database 데이터베이스명;
use 데이터베이스명;
show databases; create database 데이터베이스명; use 데이터베이스명;
show databases;

create database 데이터베이스명;

use 데이터베이스명;

도메인 분기

https://pypi.org/project/django-cors-headers/

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install django-cors-headers
pip install django-cors-headers
pip install django-cors-headers

장고 옵션 변경 – back/settings.py

서버 주소 변경

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
ALLOWED_HOSTS = []
# 변경
ALLOWED_HOSTS = ['*']
# 기존 ALLOWED_HOSTS = [] # 변경 ALLOWED_HOSTS = ['*']
# 기존
ALLOWED_HOSTS = []


# 변경
ALLOWED_HOSTS = ['*']

템플릿 디렉터리 사용

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# 변경
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'static')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# 기존 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] # 변경 import os TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'static')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
# 기존
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


# 변경
import os

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'static')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

관리자 언어 변경

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
LANGUAGE_CODE = 'en-us'
# 변경
LANGUAGE_CODE = 'ko'
# 기존 LANGUAGE_CODE = 'en-us' # 변경 LANGUAGE_CODE = 'ko'
# 기존
LANGUAGE_CODE = 'en-us'


# 변경
LANGUAGE_CODE = 'ko'

타임존 지정 – 세계 표준시(UTC)를 한국 시간으로 변경

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
TIME_ZONE = 'UTC'
# 변경
TIME_ZONE = 'Asia/Seoul'
# 기존 TIME_ZONE = 'UTC' # 변경 TIME_ZONE = 'Asia/Seoul'
# 기존
TIME_ZONE = 'UTC'


# 변경
TIME_ZONE = 'Asia/Seoul'

DB저장 시간대를 UTC기준이 아니라 한국 시간 기준으로 설정

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
USE_TZ = True
# 변경
USE_TZ = False
# 기존 USE_TZ = True # 변경 USE_TZ = False
# 기존
USE_TZ = True


# 변경
USE_TZ = False

DB 장고로 변경

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 기존
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# 변경 가이드
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name', #mysql
'USER': 'db-user-name', #root
'PASSWORD': 'db-password', #1234
'HOST': 'db-adress', #공백으로 냅두면 default localhost
'PORT': 'port-number' #공백으로 냅두면 default 3306
}
}
# 변경
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'luna',
'USER': 'root',
'PASSWORD': '내가 지정한 비밀번호',
'HOST': 'localhost',
'PORT': '3308'
}
}
# 기존 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # 변경 가이드 DATABASES = { 'default': { #'ENGINE': 'django.db.backends.sqlite3', #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': 'db-name', #mysql 'USER': 'db-user-name', #root 'PASSWORD': 'db-password', #1234 'HOST': 'db-adress', #공백으로 냅두면 default localhost 'PORT': 'port-number' #공백으로 냅두면 default 3306 } } # 변경 DATABASES = { 'default': { #'ENGINE': 'django.db.backends.sqlite3', #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': 'luna', 'USER': 'root', 'PASSWORD': '내가 지정한 비밀번호', 'HOST': 'localhost', 'PORT': '3308' } }
# 기존
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# 변경 가이드
DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db-name',         #mysql
        'USER': 'db-user-name',    #root
        'PASSWORD': 'db-password', #1234
        'HOST': 'db-adress',       #공백으로 냅두면 default localhost
        'PORT': 'port-number'      #공백으로 냅두면 default 3306
    }
}


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

        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'luna',
        'USER': 'root',
        'PASSWORD': '내가 지정한 비밀번호',
        'HOST': 'localhost',
        'PORT': '3308'
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# DATABASES 밑에 추가
CORS_ORIGIN_WHITELIST = ['http://localhost:8000',] # 도메인 분기
# DATABASES 밑에 추가 CORS_ORIGIN_WHITELIST = ['http://localhost:8000',] # 도메인 분기
# DATABASES 밑에 추가

CORS_ORIGIN_WHITELIST = ['http://localhost:8000',]  # 도메인 분기

Django 연동한 DB 초기화

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python manage.py makemigrations
python manage.py migrate
python manage.py makemigrations python manage.py migrate
python manage.py makemigrations

python manage.py migrate

HeidiSQL 로 확인 해보기


서버 실행 – 확인

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python manage.py runserver # 127.0.0.1:8000 로 확인 가능
python manage.py runserver 0.0.0.0:80 # 회사 내 192.168.. 로 확인 가능
python manage.py runserver 0.0.0.0:9999 # 이번 루나는 지정 도메인은 9999로 적어줌
python manage.py runserver # 127.0.0.1:8000 로 확인 가능 python manage.py runserver 0.0.0.0:80 # 회사 내 192.168.. 로 확인 가능 python manage.py runserver 0.0.0.0:9999 # 이번 루나는 지정 도메인은 9999로 적어줌
python manage.py runserver                   # 127.0.0.1:8000 로 확인 가능

python manage.py runserver 0.0.0.0:80        # 회사 내 192.168.. 로 확인 가능

python manage.py runserver 0.0.0.0:9999       # 이번 루나는 지정 도메인은 9999로 적어줌
Django 설치 - 장고 설치 후 화면

관리자(=슈퍼 사용자) 생성

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python manage.py createsuperuser
python manage.py createsuperuser
python manage.py createsuperuser

입력하면 프롬프트 username / email / password / password 확인 입력

Django 설치 - 슈퍼 사용자 생성

다시 서버 켜고 관리자 주소(/admin) 실행하면 접속 가능

Django 설치 - 어드민 화면