1. Admin 사이트 테이블 등록

  • 각 App에 있는 admin.py는 해당 app에 대한 내용을 "관리페이지" 에 등록할 때 작성하는 파일 임
  • 이 admin.py 모듈에 대표적으로 테이블을 등록 할 수 있음
  • 레코드 생성을 Shell 이 아닌 Django에서 지원하는 웹기반에서 편리하게 할 수 있음
  • models.py 모듈에 등록된 Table 클래스를 import 하여 등록시켜 줌
from django.contrib import admin
from .models import Book

admin.site.register(Book)

 

2. 관리자 계정 생성

  • 터미널에서 아래 명령어를 실행시켜 관리자 사이트에 접근 가능한 계정을 생성해 줌
C:\Project> python manage.py createsuperuser

 

3. HTML에 데이터 전달

  • render의 context 인자(Dictionary형)에 실어서 전달
  • 아래 코드를 분석해보면, Board Table(Class)에서 bpk의 id를 갖는 객체 b를 "b" 변수로 지정하고, b객체의 reply_set을 모두 받은 객체r을 "rlist"변수로 지정해서 context 에 실어 클라이언트에 응답 "board/detail.html"을 보냄
  • 이 변수들을 활용해서 HTML문서에 나타탤수 있게 되는 것 임
from django.shortcuts import render, redirect
from .models import Board, Reply

def detail(request, bpk) :
    b = Board.objects.get(id=bpk)
    r = b.reply_set.all()
    context = {
        "b" : b,
        "rlist" : r
    }
    return render(request, "board/detail.html", context)

 

4. Template Tag

  • 위에 서버로 부터 받은 python 구문의 변수를 사용할수 있도록 아래와 같은 Template Tag를 사용해 줌
  • {% %} : if, for, while 의 조건 구문
  • {{ }} : 객체나 변수, 함수
  • 주의할 점은 Template Tag에서는 칸 띄우기 개념이 없는 HTML문서에 표현되기 때문에 종속문장 구분이 없으므로 매 구문마다 {% endfor %}, {% endif %}와 같은 마무리 태그를 사용해야 함
<div class="container mt-5 mb-5" style="width: 80%;">
    <h1><b> 게시판 </b></h1>
    <div class="text-end"> 
        <a href="{% url 'board:create' %}" class="btn btn-dark" style="font-weight: bold;"> 글쓰기 </a>
    </div>

    <table class="table table-hover mt-3">
        <thead style="background-color: rgba(0, 0, 0, 1); color: white; font-weight: bold; font-size: 20px;">
            <tr>
                <th scope="col">NO</th>
                <th scope="col">SUBJECT</th>
                <th scope="col">SUMMARY</th>
                <th scope="col">WRITER</th>
                <th scope="col">LIKEY</th>
            </tr>
        </thead>
        <tbody>
            {% if blist %}
                {% for i in blist %}
                    <tr>
                        <th scope="row"> {{ blist.start_index|add:forloop.counter0 }} </th>
                        <td><a class="det" href="{% url 'board:detail' i.id %}"> {{ i.subject }} </a></td>
                        <td> {{ i.summary }} 
                            {% if i.reply_set.count %}
                            <sup style="color:red; font-weight:bold;"> [{{i.reply_set.count}}]</sup> 
                            {% endif %}

                        </td>
                        <td> {{ i.writer }}</td>
                        <td> {{ i.likey.count }} </td>
                    </tr>
                {% endfor %}
            {% else %}
                    {% if kw %}
                    <tr>
                        <th colspan="5"> 검색결과가 없습니다. </th>
                    </tr>
                    {% else %}
                    <tr>
                        <th colspan="5"> 첫번째 게시글을 작성해 주세요. </th>
                    </tr>
                    {% endif %}
            {% endif %}

'웹개발 > 장고 (Django)' 카테고리의 다른 글

#8_Foreign Key (외래키)  (0) 2022.06.26
#7_서버/클라이언트 간 데이터 전송  (0) 2022.06.25
#5_Model(DB)  (0) 2022.06.25
#4_HTML 기초  (0) 2022.06.25
#3_Client에 응답 보내기 (feat. MTV분리)  (0) 2022.06.25

+ Recent posts