Django Path - pai-plznw4me/django-initializer GitHub Wiki
์ฅ๊ณ ๋ templates ๊ณผ static ํ์ผ์ ์ฐพ์๋ 2๊ฐ์ง ๊ฒฝ๋ก ๋ถ๋ฅ๊ฐ ์๋ค.
ํ๋๋ default ๊ฒฝ๋ก ๋ค๋ฅธ ํ๋๋ app ๊ฒฝ๋ก์ด๋ค.
๋ฐ๋์ ๊ฒฝ๋ก ์์คํ
์ ์ดํดํด์ผ ํ๋ค. (static, templates ๊ฐ ๋์ผํ๋ค)
๊ฐ๋ น test.html ํ
ํ๋ฆฟ์ ์ฐพ๋๋ค๊ณ ๊ฐ์ ํด๋ณด์.
์ฐ์ ์ฅ๊ณ ๋ settings.py ์ ์ ์๋์ด ์๋ ๊ธฐ๋ณธ ์์น๋ฅผ ์ฐพ์ ๋ค์ด๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ์๋ค๋ฉด ์ ํด์ง ์์๋๋ก app ํ์์ ์๋ templates ํด๋๋ฅผ ์ฐพ์ ๋ค์ด๊ฐ๋ค.
๊ทธ๋์ ์ฌ๊ธฐ์ app ์ด๋ฆ ์๋ templates - app ์ด๋ฆ ์์ผ๋ก ํด๋๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
์๊ทธ๋ฌ๋ฉด django ์์ ์ ํด์ง ์์๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ ์ด๋ค app ์ ์๋ test.html ์ ๋ถ๋ฌ์ฌ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
app1
|-templates
|-test.html
app2
|-templates
|-test.html
ํ์ง๋ง ๋ง์ฝ ์๋์ ๊ฐ์ด ๊ตฌ์ฑํด ๋์ผ๋ฉด templates ์ ์ฐพ์๋ ์ ํํ๊ฒ ๋ช ์ํด ๋์์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค ๊ฐ๋ น app1/test.html ๊ทธ๋ฌ๋ฉด ๋ฌธ์ ๊ฐ ์๊ธธ์ผ์ด ์๋ค.
app1
|-templates
|-app1
|-test.html
app2
|-templates
|-app2
|-test.html
์ด๋ค App ์์ ์์ฒญ์ ํ๋ ์ฅ๊ณ ๋ templates, static ํ์ผ์ ๊ด๋ จํต์๋ ์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก templates ์ ์ฐพ๊ธฐ ๋๋ฌธ์ app1 ์์ app2 ์ ์๋ test.html ์ ์ฌ์ฉํ ๋๋ app2/test.html ์ด๋ผ๊ณ ์ ์ด ์ฃผ๋ฉด ๋๋ค!
์ฅ๊ณ ์์ ๊ฒฝ๋ก
๋ฐ url ์์น
๋ฅผ ์ฃผ๋ก ์ง์ ํ๋ค.
๊ทผ๋ฐ ๊ทธ 2๊ฐ์ง๊ฐ ํท๊ฐ๋ฆด์ ์๋ค.
๊ฐ๋จํ ์ ๋ฆฌํ์๋ฉด ํ์ผ์ ์์น๋ฅผ ์ง์ ํ ๋๋ ๊ฒฝ๋ก
์ง์
view ๋ฅผ ํตํ url ์ ๊ทผ์ url
์ง์ ์ผ๋ก ๋ถ๋ฅํด ๊ธฐ์ตํ๋ฉด ์ข๋ค.
<APP_NAME>:
<APP_NAME> ์ง์ ๊ฒฝ๋ก
# urls.py
app_name = 'school_management_app' # <<<< APP_NAME ์ง์ ๊ฒฝ๋ก
urlpatterns = [
path('student_signup', student_signup, name='student_signup'), # <<<< VIEW_NAME ์ง์ ๊ฒฝ๋ก
static ํ์ผ ๋๋ render ์ ์ด๋ค templates file ์ ๋ถ๋ฌ์ฌ์ง ๊ฒฐ์ ํ ๋ ์ฌ์ฉํ๋ค. ์๋ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๋๋ฐ django ์์ ๊ธฐ์ค ๊ฒฝ๋ก๊ฐ ์ด๋์ธ์ง ์์์ผ ํ๋ค.
|-project
|-app
|- templates <-- templates ํ์ผ ๊ธฐ์ค ๊ฒฝ๋ก
|-app
|-index.html
|- static # <-- static file ๊ธฐ์ค ๊ฒฝ๋ก
|-js
|- scrollUp.js
๊ธฐ์ค ๊ฒฝ๋ก๋ /templates ํด๋๊ฐ ๊ธฐ์ค ๊ฒฝ๋ก์ด๋ค.
๊ฐ๋ น render function ์ผ๋ก index.html ์ ์ฐธ์กฐํ๋ค๊ณ ํด๋ณด์.
๊ทธ๋ผ ์๋ ์ฝ๋์ ๊ฐ์ด ๋์ด์ผ ํ๋ค.
render(request, template_name='app/index.html')
์ฐ๋ฆฌ๋ ์๋ฒ๋ฅผ manage.py ๊ฒฝ๋ก์์ ์์ํ๊ธฐ ๋๋ฌธ์ python ์์ static file ์ ๋ถ๋ฌ์ค๋๊ฑด ์ด๋ ต์ง ์๋ค.
ํ์ง๋ง html ์์ static ํ์ผ์ ๋ถ๋ฌ์ฌ๋๊ฐ ๋ฌธ์ ์ธ๋ฐ
html ์์ static file ์ ๋ถ๋ฌ์ฌ๋์ ๊ฒฝ๋ก๋ templates ๊ฒฝ๋ก์ ์ ์ฌํ๋ค.
๊ธฐ์ค ๊ฒฝ๋ก๋ <APP>/static
ํด๋๊ฐ ๊ธฐ์ค ๊ฒฝ๋ก์ด๋ค.
์ ์์์์ scrollUp.js
์ ๋ถ๋ฌ์จ๋ค๊ณ ํ์๋ ์๋์ ๊ฐ์ด ๋ถ๋ฌ์ฌ์ ์๋ค.
{% load static %}
<script src="{% static 'app/js/scrollUp.js' %}"></script>