Django - 실습편 기본세팅 (1)

아카이브 · 2021. 6. 8. 14:49

Django 기본앱 생성

 

Django app 생성

python manage.py startapp blog

settings 등록

base/settings/common.py

LOCAL_APPS = [
    "blog"
]

Blog app에 url.py 생성

blog/urls.py

from django.urls import path
from . import views

urlpatterns = [
]

Base app에 blog url 등록

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include("blog.urls"), name="blog")
]

 

Django settings file 분리

base/settings/common.py - example

DJANGO_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

THIRD_PARTY_APPS = [
    "rest_framework",
    "corsheaders"
]

LOCAL_APPS = [
    "user",
    "blog",
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

base/settings/dev.py - example

from .common import *

INSTALLED_APPS += ["django_extensions"]

CORS_ORIGIN_WHITELIST = [
    "http://localhost:3000",
    "http://127.0.0.1:3000",
    "http://localhost:8000",
    "http://127.0.0.1:8000",
    "http://localhost:8001",
    "http://127.0.0.1:8001",
]

CORS_ALLOWED_ORIGINS = [
    "http://localhost:3000",
    "http://127.0.0.1:3000",
    "http://localhost:8000",
    "http://127.0.0.1:8000",
    "http://localhost:8001",
    "http://127.0.0.1:8001",
]

base/settings/prod.py - example

from .common import *

DEBUG = os.environ.get("DEBUG") in ["1", "t", "true", "T", "True"]

CORS_ORIGIN_WHITELIST = os.environ.get("CORS_ORIGIN_WHITELIST", "").split(",")

CORS_ALLOWED_ORIGINS = os.environ.get("CORS_ORIGIN_WHITELIST", "").split(",")


 

 

requirements.txt  별도 분리

 

requirements/common.txt

Django==3.2.3
asgiref==3.3.4
pytz==2021.1
sqlparse==0.4.1
djangorestframework~=3.11.1
django-cors-headers
djangorestframework-jwt
django-debug-toolbar
psycopg2-binary
PyYAML==5.4.1

requirements/common.txt

-r common.txt

django-debug-toolbar
django_extensions

requirements/prod.txt

-r common.txt

gunicorn

 

ASGI, WSGI 기본 디폴트 값 세팅

 

asgi - wsgi는 비동기를 사용할 수 없음 이 한계를 극복하기 위해서 나옴

 

base/asgi.py

- 기본값을 dev 버전으로 세팅 (base.settings.dev)

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'base.settings.dev')

application = get_asgi_application()

 

base/wsgi.py

- 기본값을 dev 버전으로 세팅 (base.settings.dev)

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'base.settings.dev')

application = get_wsgi_application()

 

manage.py 기본값세팅

 

- 기본값을 dev 버전으로 세팅 (base.settings.dev)

import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'base.settings.dev')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()