728x90
반응형
SMALL
main.html
장고에서는 base 폴더 안에 템플릿을 추가하도록 권장하고 있다.
<!DOCTYPE html>
<html>
<head>
<meta charset = 'utf-8'>
<meta http-equiv = 'X-UA-Compatible' content = 'IE=edge'>
<title>DreamWell</title>
<meta name = 'viewport' content = 'width=device-width, initial-scale=1'
</head>
<body>
{% include 'navbar.html' %}
{% block content %}
{% endblock %}
</body>
</hmtl>
home.html
{% extends 'main.html' %}
{% block content %}
<h1>Home Template</h1>
<div>
<div>
{% for room in rooms %}
<div>
<h5>{{room.id}} -- <a href = "{% url 'room' room.id %}">{{room.name}}</a></h5>
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
room.html
{% extends 'main.html' %}
{% block content %}
<h1>{{room.name}}</h1>
{% endblock content %}
views.py
base 폴더의 views.py를 다음과 같이 작성한다.
from django.shortcuts import render
rooms = [
{'id':1, 'name':'Hi'},
{'id':2, 'name':'Welcome!'}
]
def home(request):
context = {'rooms': rooms}
return render(request, 'base/home.html', context)
def room(request, pk):
room = None
for i in rooms:
if i['id'] == int(pk):
room = i
context = {'room': room}
return render(request, 'base/room.html', context)
urls.py
base 폴더의 urls.py를 다음과 같이 작성한다.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = "home"),
path('room/<str:pk>/', views.room, name = "room"),
]
728x90
반응형
LIST
'App Programming > Django' 카테고리의 다른 글
[Django] CRUD (0) | 2023.02.17 |
---|---|
[Django] Admin (Room) (0) | 2023.02.17 |
[Django] 템플릿 (Templates) (1) (0) | 2023.02.01 |
[Django] URL's / Views (0) | 2023.02.01 |
[Django] 환경 설정 (0) | 2023.02.01 |