๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“  archive

Vue-Router history mode ์ง€์›์„ ์œ„ํ•œ Django ์„ค์ •

by HandHand 2021. 3. 1.

Vue-router์™€ Django

Vue.js์—์„œ vue-router ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ URL์— # ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•ด์‹œ๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํžˆ์Šคํ† ๋ฆฌ ๋ชจ๋“œ๋ฅผ ์ ์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ Django๋ฅผ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋กœ ๋‘๊ณ  ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค.

๐Ÿ’ก ๋ฌธ์ œ์ 

๋งŒ์•ฝ Vue.js์—์„œ ํžˆ์Šคํ† ๋ฆฌ ๋ชจ๋“œ๋ฅผ ์ ์šฉํ•œ ํ›„ npm build ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ๋นŒ๋“œ ํŒŒ์ผ์„

Django์˜ index.html ๋กœ ์„ค์ •ํ•œ ๋‹ค์Œ ์ง์ ‘ URL๋กœ ํŠน์ • ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๊ฒฝ์šฐ 404 ์—๋Ÿฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Š” Vue.js๊ฐ€ SPA(Single Page Application)์ด๋ผ์„œ Django ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ • URL ํŒจํ„ด์„ ์•Œ ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์—์„œ ๋ฒŒ์–ด์ง„ ์ผ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด Django์—์„œ ํžˆ์Šคํ† ๋ฆฌ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

Django์—์„œ History mode ์ง€์›์„ ์œ„ํ•œ ์ž‘์—…

Django์—์„œ ํžˆ์Šคํ† ๋ฆฌ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•ฝ๊ฐ„์˜ ํŠธ๋ฆญ์„ ์ด์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด urls.py ์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ url ํŒจํ„ด์„ ์ •๊ทœํ‘œํ˜„์‹์„ ํ™œ์šฉํ•ด ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งค์นญ๋˜์ง€ ๋ชปํ•œ URL์€ ๋‹ค์‹œ SPA ๋‚ด์—์„œ ์›ํ•˜๋Š” ํŽ˜์ด์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

config/urls.py


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

from django.views.generic import TemplateView

urlpatterns = [
    path('', TemplateView.as_view(template_name='index.html')),
    path('admin/', admin.site.urls),

    # shopping app urls
    path('shopping/', include('shopping.urls')),
    # ... rest of your urls
]

# history mode ์ง€์›
urlpatterns += [
     re_path('^.*$', TemplateView.as_view(template_name='index.html')),
]

์ฐธ๊ณ  ์ž๋ฃŒ

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€