Variable Routing & DTL

Django - The web framework for perfectionists with deadlines

Folder structure

1. Package folder

settings.py

Allowed hosts

ALLOWED_HOSTS = ['*']
  • '*' ์€ ๋ชจ๋“  ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” wildcard

Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages', #app register
]
  • ์ƒˆ๋กœ์šด app์„ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•จ

Language setting & Internalization

LANGUAGE_CODE = 'ko-kr' 

TIME_ZONE = 'Asia/Seoul'

#Internalization
USE_I18N = True 
USE_L10N = True
USE_TZ = True

manage.py

  • ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํŒŒ์ผ

  • ์ˆ˜์ •ํ•˜์ง€ ๋ง๊ฒƒ!

2. Apps folder

  • apps.py

    : app ์„ค์ •

  • admin.py

    : ๊ด€๋ฆฌ์ž view

  • models.py

    : model

  • tests.py

    : ํ…Œ์ŠคํŠธ

Variable routing

url์˜ ํŠน์ • ์œ„์น˜์˜ ๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ํ™œ์šฉ

1. urls.py

# django_intro/urls.py
path('hi/<str:name>/', views.hi),
path('add/<int:a>/<int:b>/', views.add),

2. views.py

# pages/views.py
def hi(request, name):
    context = {
        'name':name
    }
    return rnder(request, 'hi.html', context)

3. template

<!-- pages/templates/hi.html -->
<h1>
    Hi, {{name}}
</h1>

DTL (Django Template Language)

Template file (HTML) ์€ django template language๋ฅผ ํ†ตํ•ด ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค!

  • Djangoโ€™s template language is designed to strike a balance between power and ease.

  • Itโ€™s designed to feel comfortable to those used to working with HTML.

๊ธฐ๋ณธ ๋ฌธ๋ฒ•

1. ์ถœ๋ ฅ {{ }}

{{ name }}
{{ menu.0 }}

2. ๋ฌธ๋ฒ• {% %}

{% for option in options%}

{% endfor %}

Last updated