728x90
반응형
SMALL
URLconf
books 앱 폴더에 urls.py 파일을 만들고 각 페이지에 맞는 URL을 매치한다.
from django.urls import path
from . import views
app_name = 'books'
urlpatterns = [
path('', views.BooksModelView.as_view(), name='index'),
path('book/', views.BookList.as_view(), name='book_list'),
path('author/', views.AuthorList.as_view(), name='author_list'),
path('publisher/', views.PublisherList.as_view(), name='publisher_list'),
]
클래스 뷰
각 URL에 맞는 기능을 구현한다.
from django.views.generic import TemplateView, ListView, DetailView
from books.models import Book, Author, Publisher
class BooksModelView(TemplateView):
template_name = 'books/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['model_list'] = ['Book', 'Author', 'Publisher']
return context
class BookList(ListView):
model = Book
class AuthorList(ListView):
model = Author
class PublisherList(ListView):
model = Publisher
class BookDetail(DetailView):
model = Book
class AuthorDetail(DetailView):
model = Author
class PublisherDetail(DetailView):
model = Publisher
템플릿
URL 패턴 | 뷰 클래스명 | 템플릿 파일명 | 템플릿 설명 |
books/ | BooksModelView | index.html | books 앱 첫 화면 |
books/book/ | BookList | book_list.html | 책 리스트 보여줌 |
books/author/ | AuthorList | author_list.html | 저자 리스트 보여줌 |
books/publisher/ | PublisherList | publisher_list.html | 출판사 리스트 보여줌 |
books/book/99 | BookDetail | book_detail.html | 특정 책의 상세 정보 보여줌 |
books/author/99/ | AuthorDetail | author_detail.html | 특정 저자의 상세 정보 보여줌 |
books/publisher/99/ | PublisherDetail | publisher_detail.html | 특정 출판사의 상세 정보 보여줌 |
<!-- index.html -->
{% extends "base_books.html" %}
{% block content %}
<h1>Book Management System</h1>
<ul>
{% for modelname in model_list %}
{% with "books:"|add:modelname|lower|add:"_list" as url_var %}
<!-- books:book_list books:author_list books:publisher -->
<li><a href="{% url url_var %}">{{ modelname }}</a></li>
{% endwith %}
{% endfor %}
</ul>
{% endblock %}
<!-- book_list.html -->
{% extends "base_books.html" %}
{% block content %}
<h2>Book List</h2>
<ul>
{% for book in object_list %}
<li>
<a href="{% url 'books:book_detail' book.id %}">
{{ book.title }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
<!-- book_detail.html -->
{% extends "base_books.html" %}
{% block content %}
<h1>{{ object.title }}</h1>
<ul>
<li>
Authors:
{% for author in object.authors.all %}
{{ author }}
{% if not forloop.last %}, {% endif %}
{% endfor %}
</li>
<li>
Publisher: {{ object.publisher }}
</li>
<li>
Publication date: {{ object.publication_date }}
</li>
</ul>
{% endblock content %}
<!-- author_list.html -->
{% extends "base_books.html" %}
{% block content %}
<h2>Author List</h2>
<ul>
{% for author in object_list %}
<li>
<a href="{% url 'books:author_detail' author.id %}">
{{ author.name }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
<!-- author_detail.html -->
{% extends "base_books.html" %}
{% block content %}
<h1>{{ object.name }}</h1>
<ul>
<li>{{ object.salutation }}</li>
<li>Email: {{ object.email }}</li>
</ul>
{% endblock content %}
<!-- publisher_list.html -->
{% extends "base_books.html" %}
{% block content %}
<h2>Publisher List</h2>
<ul>
{% for publisher in object_list %}
<li>
<a href="{% url 'books:publisher_detail' publisher.id %}">
{{ publisher.name }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
<!-- publisher_detail.html -->
{% extends 'base_books.html' %}
{% block content %}
<h1>{{ object.name }}</h1>
<ul>
<li>{{ object.website }}</li>
<li>Address: {{ object.address }}</li>
</ul>
{% endblock content %}
html 상속
myproject 프로젝트에서 templates 폴더를 생성한다. HTML 파일에서 Header와 Footer, Sidebar와 같이 많은 페이지에 반복적으로 사용되는 내용들을 계속해서 반복적으로 작성하는 것은 비효율적이다. 이 부분을 쉽게 해결하기 위해 상속 기능을 사용한다.
# base_books.html
{% extends "base.html" %}
<title>{% block title %}Books Application Site{% endblock title %}</title>
{% block sidebar %}
{{ block.super }}
<ul>
<li><a href="/books/">Books Home</a></li>
</ul>
{% endblock sidebar %}
가져다 쓸 base 파일들을 만들어 두고 extends를 사용하여 상속받은 후 사용할 내용을 붙여준다. {% block 변수명 %} {% endblock 변수명 %}을 사용해서 해당부분을 대체한다.
728x90
반응형
LIST
'App Programming > Django' 카테고리의 다른 글
[Django] 북마크 앱 만들기 (1) (0) | 2023.06.28 |
---|---|
[Django] 웹 서버 연동 원리 (0) | 2023.06.28 |
[Django] Books 어플리케이션 (1) (0) | 2023.06.27 |
[Django] 폼 (Form) (0) | 2023.06.27 |
[Django] 템플릿 시스템 (2) (0) | 2023.06.26 |