Skip to content

Latest commit

ย 

History

History
98 lines (85 loc) ยท 3.36 KB

File metadata and controls

98 lines (85 loc) ยท 3.36 KB

urls.py ์ •๋ฆฌ

์ด์ œ url์„ app๋งˆ๋‹ค ๊ฐ๊ฐ ๊ด€๋ฆฌํ•˜๊ณ  ์ด๋ฅผ ํ”„๋กœ์ ํŠธ ํด๋”์˜ urls.py์—์„œ includeํ•ด์„œ ์‚ฌ์šฉํ• ๊ฒ๋‹ˆ๋‹ค.

๐Ÿ’กapp๋งˆ๋‹ค urls.py๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ด์œ ?

ํ”„๋กœ์ ํŠธ urls.py์— ๋ชจ๋“  path๋ฅผ ์ ๋Š”๋‹ค๋ฉด ํŽ˜์ด์ง€๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก path์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ•

  1. app(main)์— urls.py ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ
  2. app์˜ urls.py์— path๋ฅผ ์ ์–ด์ฃผ๊ธฐ

โ‚ฉโ‚ฉโ‚ฉ from django.urls import path from .views import *

app_name = "main"

from django.urls import path
from .views import *

urlpatterns = [
    path('', showmain, name="showmain"),
    path('first/', first, name="first"),
    path('second/', second, name="second"),
    path('<str:id>', detail, name="detail"),
    path('new/', new, name="new"),
    path('create/', create, name="create"),
    path('posts/', posts, name="posts"),
    path('edit/<str:id>', edit, name="edit"),
    path('update/<str:id>', update, name="update"),
    path('delete/<str:id>', delete, name="delete"),
]
  1. basic/urls.py์—์„œ main app์˜ urls.py์— ์ ์–ด์ค€ path๋ฅผ ์ง€์›Œ์ฃผ๊ณ  includeํ•ด์ฃผ๊ธฐ
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls')),
] 
  1. app_name ์„ค์ •ํ•ด์ฃผ๊ธฐ
from django.urls import path
from .views import *

app_name = "main"
urlpatterns = [
    path('', showmain, name="showmain"),
    path('first/', first, name="first"),
    path('second/', second, name="second"),
    path('<str:id>', detail, name="detail"),
    path('new/', new, name="new"),
    path('create/', create, name="create"),
    path('posts/', posts, name="posts"),
    path('edit/<str:id>', edit, name="edit"),
    path('update/<str:id>', update, name="update"),
    path('delete/<str:id>', delete, name="delete"),
]
  1. template์˜ url๋ถ€๋ถ„ ์ˆ˜์ •ํ•ด์ฃผ๊ธฐ
<a href="{% url 'main:first' %}">first</a>
<a href="{% url 'main:second' %}">second</a>

์•ฑ ์ด๋ฆ„:url ์ด๋ฆ„ ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ์–ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’กapp_name์„ ์„ค์ •ํ•˜๋Š” ์ด์œ ?

์—ฌ๋Ÿฌ ์•ฑ์—์„œ ๋™์ผํ•œ ์ด๋ฆ„์˜ url์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋  ๊ฒฝ์šฐ app_name์„ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์›ํ•˜๋Š” url๋กœ ์ด๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์šฐ๋ฆฌ๋Š” main์— showmain์ด๋ผ๋Š” url์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ profile์ด๋ผ๋Š” ์ƒˆ๋กœ์šด app์„ ๋งŒ๋“ค์—ˆ๊ณ , ๊ทธ ์•ฑ์˜ ๋ฉ”์ธ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด showmain์ด๋ผ๋Š” ์ด๋ฆ„์˜ url์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒ ์ฃ .

# profile/urls.py (์˜ˆ์‹œ)
from django.urls import path
from .views import *

urlpatterns = [
    path('', showmain, name="showmain"),
]

์ด๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ main์•ฑ์˜ ๋ฉ”์ธ๊ณผ profile์•ฑ์˜ ๋ฉ”์ธ์„ ๋ณด์—ฌ์ฃผ๋Š” ๋งํฌ๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

<a href="{% url 'showmain' %}">main์•ฑ์˜ ๋ฉ”์ธํ™”๋ฉด</a>
<a href="{% url 'showmain' %}">profile์•ฑ์˜ ๋ฉ”์ธํ™”๋ฉด</a>

๋ณด๋‹ค์‹œํ”ผ ๊ตฌ๋ถ„์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. django ๋˜ํ•œ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†์–ด ๋‘˜ ์ค‘ ์–ด๋Š showmain url๋กœ ์ด๋™ํ• ์ง€ ํ™•์‹คํžˆ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ฐ๊ฐ app_name์„ main ๊ณผ profile๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค๋ฉด

<a href="{% url 'main:showmain' %}">main์•ฑ์˜ ๋ฉ”์ธํ™”๋ฉด</a>
<a href="{% url 'profile:showmain' %}">profile์•ฑ์˜ ๋ฉ”์ธํ™”๋ฉด</a>

์œ„์™€ ๊ฐ™์ด ๋˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์›ํ–ˆ๋˜ url๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.