hyeonga_code

파이선 웹구축_장고_06_URL_분리 본문

Python_Django

파이선 웹구축_장고_06_URL_분리

hyeonga 2023. 5. 29. 05:59
반응형

- URL_분리

- 시간 단축을 위함

- render

1) 'settings.py'

> Template = [ DIR = [BASE_DIR/'templates']]

2) 'urls.py'

> from 앱이름 import views

> path('함수이름/', views.'참조파일.html')

3) <templates> > <앱이름> > '참조파일.html'

> <h3>참조파일 내용 입력 </h3>

 

1. <바탕폴더> 폴더에 <templates> 폴더 생성

2. <templates> 폴더에 <앱이름> 폴더 생성

- '참조파일.html' 이동

 

3. <config> > 'settings.py'

'settings.py'

=====

1
2
3
4
5
6
7
8
9
10
11
12
 
TEMPLATES = [
 
{
 
'BACKEND''django.template.backends.django.DjangoTemplates',
 
'DIRS': [ 'BASE_DIR/templates' ],
 
----------< 입력 >---------------
 
 
cs

 

 

4. <앱이름> > 'views.py'

'views.py'

=====

1
2
3
4
5
6
7
 
from django.shortcuts import render
 
 
def 함수이름(request):
 
return render(request, '앱이름/참조파일.html')
cs

 

 

- url 분리하기 2

1. <config> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# path('blog/', include('blog.urls'))
 
_ 보고 따라하기
 
 
from django.contrib import admin
 
from django.urls import path, include
 
 
urlpatterns = [
path('admin/', admin.site.urls),
path('앱이름/', include('앱이름.urls'))
]
cs

 

 

 

2. <앱이름> > 'urls.py' 파일 생성

'urls.py'

=====

1
2
3
4
5
6
7
8
9
10
11
 
from django.urls import path
 
from . import views
 
 
urlpatterns = [
 
path('함수이름/', views.함수이름)
 
]
cs

 

>> 실행됨

- 문제 없는데 실행이 안 되는 경우

- runserver 재실행

 

- 버튼으로 경로 변경하기

1) <a href='a'><button>''/' 없을 때</button></a>

>> http://127.0.0.1:8000/앱이름/함수이름/a

# /로 시작하지 않으면 마지막으로 /가 닫힌 현재 경로로부터

표시됨

2) <a href="/a"><button>돌아가기</button></a>

>> http://127.0.0.1:8000/앱이름/

 

반응형