DB에 저장한 데이터 template으로 가져오고 테이블 꾸미기

2024. 1. 28. 17:17·django
728x90

1. view 함수에서 저장한 데이터 가져오기

1) Model에 있는 burger 클래스를 가져오고 해당 데이터를 template으로 전달해주기 위해 dictionary 객체를 사용

dictionary 객체의 변수명은 context로 지정

context dict 객체 안에 burgers라는 키를 지정해주고, 해당 키의 값은 Burger.objects.all()로 설정

 

2. template에서 전달된 데이터 출력

딕셔너리로 받은 키값을 불러올땐 {{ key }}로 확인

 

페이지 확인해보면 이렇게 뜸

 

2-1. 햄버거 이름, 가격, 칼로리 함께 표시하기

{%for%} 태그 활용하여 burger 클래스에 있는 각 항목 불러오기

 

 

2-2. 테이블 꾸미기

style 태그는 스타일을 정의하는 태그, table은 표를 나타내는 태그

<!DOCTYPE html>
<html lang="ko">
    <body>
        <h1>햄버거 목록입니다</h1>
            <div>
                <style>
                    table {
                        border-collapse: collapse;
                        width: 60%;
                        border-spacing: 0;
                    }
                
                    th, td {
                        border: 1px solid #000000;
                        padding: 8px;
                        text-align: center;
                    }
                
                    th {
                        background-color: #000000;
                        font-weight: bold; /* 굵게 설정하는 부분 */
                        color: #ffffff; /* 글자색을 흰색으로 설정 (선택사항) */
                    }
                </style>

                <table> 
                    <tr>
                        <th>name</th>
                        <th>price</th>
                        <th>calories</th>
                    </tr>
                    {% for burger in burgers %}
                        <tr>
                            <td>{{burger.name}}</td>
                            <td>{{burger.price}}원</td>
                            <td>{{burger.calories}}kcal</td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
    </body>
</html>


부트스트랩 사용해서 테이블 꾸미기

1. views 파일에서 데이터를 pandas 데이터 프레임으로 불러오고 부트스트랩에서 원하는 테이블 class를 불러옴

 

2. html 파일에서 head 영역에 부트스트랩 css 코드 넣어주고 body 영역에 {{  burgers|safe }} 넣어줌 

<!DOCTYPE html>
<html lang="ko">
<head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
    <body>
        <h1>햄버거 목록입니다</h1>
        {{ burgers|safe }}
    </body>
</html>

 

728x90

'django' 카테고리의 다른 글

model 클래스 만들고 관리자 페이지 만들기  (1) 2024.01.28
view, template, URLconf 만들고 연결하기  (0) 2024.01.28
[Django] 설치 및 프로젝트 생성  (0) 2024.01.27
Django 디자인패턴 MTV, get&post  (0) 2024.01.27
'django' 카테고리의 다른 글
  • model 클래스 만들고 관리자 페이지 만들기
  • view, template, URLconf 만들고 연결하기
  • [Django] 설치 및 프로젝트 생성
  • Django 디자인패턴 MTV, get&post
GinaKim
GinaKim
안녕하세요! 반갑습니다 :)
  • GinaKim
    디디
    GinaKim
  • 전체
    오늘
    어제
    • 분류 전체보기 (91)
      • Python (43)
        • Python 기초문법 (25)
        • 데이터 시각화 (5)
        • 통계 (8)
        • 크롤링 (5)
      • git (5)
      • streamlit (5)
      • django (5)
      • 머신러닝 (18)
      • Spark (4)
      • Google Cloud Platform (8)
      • Tableau (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
GinaKim
DB에 저장한 데이터 template으로 가져오고 테이블 꾸미기
상단으로

티스토리툴바