Step#6-1. 생성된 APP을 사용하기 위한 설정 (urls.py, 상위의 settings.py)

정은경·2019년 12월 23일
0

🎸 Play the Django 

목록 보기
12/57

1) 생성된 APP의 존재를 장고에게 알려요!

Step#6에서 "user"라는 이름의 APP을 생성하였는데요!
생성된 APP의 존재를 장고에게 알리기 위하여,
장고로 생성한 프로젝트 디렉터리 안의 settings.py에다가 아래와 같이
"INSTALLED_APPS"에 생성된 APP 이름을 추가합니다.
image.png

(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── manage.py
├── mynewjangodirec
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── settings.cpython-37.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── user
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

4 directories, 15 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./mynewjangodirec/settings.py
"""
Django settings for mynewjangodirec project.

Generated by 'django-admin startproject' using Django 3.0.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u8(7%^1zs80hb(93lyxg_+s=+a12b465m^*lk4e$8bar4v&oll'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mynewjangodirec.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mynewjangodirec.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },

    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » vi ./mynewjangodirec/settings.py
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »                                                             
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./mynewjangodirec/settings.py                                                                                                                                                                            1 ↵
"""
Django settings for mynewjangodirec project.

Generated by 'django-admin startproject' using Django 3.0.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u8(7%^1zs80hb(93lyxg_+s=+a12b465m^*lk4e$8bar4v&oll'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'user',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mynewjangodirec.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mynewjangodirec.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »

2) 생성된 APP의 URL 설정

우선, 생성한 장고 프로젝트 디렉터리 안에 있던 urls.py 파일을
생성한 APP 디렉터리 안으로 복사를 할게요!
생성한 APP 디렉터리 안에 urls.py 파일을 생성하려고 하는데
새로 만들 수도 있지만 복사가 편하니 복사하겠습니다.
복사한 뒤에는 기존에 생성된 장고 프로젝트 디렉터리만을 위한 urls 설정은 지워줍니다.
왜냐하면 APP 디렉터리 안의 urls.py 용도로 쓸 거니깐요!

(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd                                                                                                                                                                                                        127 ↵
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── manage.py
├── mynewjangodirec
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── settings.cpython-37.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── user
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

4 directories, 15 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./mynewjangodirec/urls.py
"""mynewjangodirec URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cp ./mynewjangodirec/urls.py ./user/.
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── manage.py
├── mynewjangodirec
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── settings.cpython-37.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── user
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    ├── urls.py
    └── views.py

4 directories, 16 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/urls.py
"""mynewjangodirec URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » vi ./user/urls.py
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/urls.py
"""mynewjangodirec URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import path

urlpatterns = [
    path('', ),
]
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글