-
게시판 예제 - 댓글Python Django 2022. 10. 23. 15:20
veiws2.py
from django.shortcuts import render, redirect from myboard.models import BoardTab from datetime import datetime # Create your views here. def replyFunc(request): try: data=BoardTab.objects.get(id=request.GET.get('id')) context={'data_one':data} return render(request, 'rep/reply.html',context) except Exception as e: print('replyFunc err : ', e) return render(request, 'error.html') def replyOkFunc(request): if request.method == 'POST': try: repGnum=int(request.POST.get('gnum')) repOnum=int(request.POST.get('onum')) imsiRec=BoardTab.objects.get(id=request.POST.get('id')) oldGnum=imsiRec.gnum oldOnum=imsiRec.onum if oldGnum==repGnum: oldOnum=oldOnum + 1 #답글 저장 BoardTab( name=request.POST.get('name'), passwd=request.POST.get('passwd'), mail=request.POST.get('mail'), title=request.POST.get('title'), cont=request.POST.get('cont'), bip=request.META['REMOTE_ADDR'], #글을 입력한 사람(클라이언트)의 컴퓨터 아이피 bdate=datetime.now(), readcnt=0, gnum=repGnum, onum=oldOnum, nested=int(request.POST.get('nested'))+1 #들여쓰기 ).save() return redirect('/board/list') #답글 작성 후 목록 보기 except Exception as e: print('replyOkFunc err : ', e) return render(request, 'error.html')
board.html <댓글 추가>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" /> </head> <body style="margin:20px;"> <div style="width:95%; text-align: center;"> <h3>** 게시판 목록 **</h3> </div> <a href="/board/insert">글쓰기</a> <table style="width: 95%;" class="table"> <tr> <th>번호</th> <th>제 목</th> <th>작성자</th> <th>작성일</th> <th>조회수</th> </tr> {% if datas %} {% for d in datas %} <tr> <td>{{d.id}}</td> <td> <!-- 들여쓰기 = 가지고 있는 d.nested의 숫자만큼 반복해서 띄어쓰기한다. 변수를 주지 않은 이유는 단순히 반복문만 돌릴것이기 때문이다.--> {% with ''|center:d.nested as range %} {% for _ in range %} {% endfor %} {% endwith %} <a href="/board/content?id={{d.id}}&page={{datas.number}}">{{d.title}}</a> </td> <td>{{d.name}}</td> <td>{{d.bdate.year}}.{{d.bdate.month}}.{{d.bdate.day}}</td> <td>{{d.readcnt}}</td> </tr> {% endfor %} <!-- 페이징 처리 --> <tr> <td colspan="5" style="text-align: center;"> {% if datas.paginator.num_pages > 1 %} <div> {% if datas.has_previous %} <!-- 이전 페이지가 있다면 --> <a href="/board/list?page={{datas.previous_page_number}}">«이전</a> {% endif %} {% if datas.has_next %} <!-- 이전 페이지가 있다면 --> <a href="/board/list?page={{datas.next_page_number}}">다음»</a> {% endif %} (페이지:{{datas.number}} / {{datas.paginator.num_pages}}) </div> {% endif %} </td> </tr> {% else %} <tr> <td colspan="5">자료가 없습니다.</td> </tr> {% endif %} <!-- 검색 기능 --> <tr> <td colspan="5" style="text-align: center;"> <form action="/board/search" method="post">{% csrf_token %} 검색 : <select name="s_type"> <option value="title" selected>글제목</option> <option value="name" selected>작성자</option> </select> <input type="text" name="s_value"/> <input type="submit" value="확인" /> </form> </td> </tr> </table> </body> </html>
reply.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" /> <script> window.onload = function(){ document.querySelector("#btnReplyok").onclick = chkFunc; } function chkFunc(){ // alert('a'); if(frm.name.value === ""){ alert('작성자를 입력하시오'); frm.name.focus() return; } // 나머지는 생략 frm.submit(); } </script> </head> <body style="margin:20px;"> <h2>* 답글 쓰기 *</h2> <form action="/board/replyok" method="post" name="frm">{% csrf_token %} <input type="hidden" name="id" value="{{data_one.id}}"/> <input type="hidden" name="gnum" value="{{data_one.gnum}}"/> <input type="hidden" name="onum" value="{{data_one.onum}}"/> <input type="hidden" name="nested" value="{{data_one.nested}}"/> <table style="width: 95%;" class="table"> <tr> <td>작성자:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>비밀번호:</td> <td><input type="text" name="passwd"/></td> </tr> <tr> <td>이메일:</td> <td><input type="email" name="mail"/></td> </tr> <tr> <td>글제목:</td> <td><input type="text" style="width: 99%" name="title" value="[RE]{{data_one.title}}"/></td> </tr> <tr> <td>글내용:</td> <td><textarea rows="5" style="width: 99%" name="cont"></textarea></td> </tr> <tr> <td colspan="2" style="text-align: center;"> <input type="button" value="답글 등록" id="btnReplyok" class="btn btn-primary"/> <input type="button" value="이전 화면" onclick="history.back()" class="btn btn-success"/> </td> </tr> </table> </form> </body> </html>
'Python Django' 카테고리의 다른 글
Python Django 18 - Python Ajax (0) 2022.10.23 예제(table 가져와서 join해서 결과 나타내기) (0) 2022.10.23 게시판 예제 (0) 2022.10.23 Python Django 17 - table이 존재하는 상황(table 가져오기) (0) 2022.10.23 Python Django 16 - join 복수의 DB 사용(ORM) (0) 2022.10.23