Pet-Project-web-게시글 추가 기능 및 날짜 추가

개요

웹에 클론코딩 페이지를 추가하기까지의 정리

현황

게시글 추가와 게시글에 날짜를 추가하였다.
게시글 화면

[글쓰기 버튼 추가 및 작성일시 추가]
게시글 작성 화면
[글쓰기 눌렀을 때 화면]

소스 코드 수정 현황

  1. clone_board.py
  2. board.html

clone_board.py 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from threading import main_thread
from flask import Blueprint, request, render_template, flash, redirect, url_for, g,session
from app.module.dbModule import Database
from app.forms.forms import ContentAddForm, UserAddCheck
from datetime import datetime

clone_board_bp = Blueprint('clone_board',__name__,url_prefix='/clone_board')

@clone_board_bp.route('/list',methods=['POST','GET'])
def list():
db = Database()
content_list = db.executeAll("SELECT * FROM board_content_table")
print(content_list)
return render_template('/main/board.html',content_list=content_list)



@clone_board_bp.route('/add',methods=['POST','GET'])
def add():
form = ContentAddForm()
title = form.content_title.data
text = form.content_text.data
if request.method == 'POST' and not form.validate_on_submit() and ((title == "") or (text == "")):
error = "데이터 양식이 맞지 않습니다"
flash(error)
elif request.method == 'POST' and not((title == "") or (text == "")):
db = Database()
db.execute("""INSERT INTO board_content_table (board_content,board_content_title,write_time) VALUES ('%s','%s','%s')""" % (text, title,datetime.now()))
db.commit()
return redirect(url_for("clone_board.list"))

return render_template('/main/board_add.html',form=form)

clone_board.py 설명

  1. 게시글 리스트를 보여주는 list 페이지와 게시글 추가 버튼을 눌렀을 때의 add 페이지로 분리
  2. 게시글 리스트 페이지에서는 항상 디비로부터 가져온 정보를 모두 출력함(아직 페이징 기능 미구현)
  3. 게시글 추가 페이지에서는 제목과 내용을 입력하면 데이터베이스에 추가함
    • 제목이나 내용을 입력하지 않으면 에러 출력함
  4. 데이터베이스에 제대로 입력됐다면 list 페이지로 재연결해줌

    clone_board.html 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{% extends 'main/clone_base.html' %}
{% block content %}

<html>
<body>
<div class="container my-3">
<table class="table">
<thead>
<tr class="thead-dark">
<th>번호</th>
<th>제목</th>
<th>작성일시</th>

</tr>
</thead>
<tbody>
{% if content_list %}
{% for content in content_list %}
<tr>
<td>{{ content['board_content_idx'] }}</td>
<td>
{{ content['board_content'] }}
</td>
<td>{{ content['write_time'] }}</td>

</tr>


</table>
<div class="btns">
<li><a href="/clone_board/add" class="button big">글쓰기</a></li>
</div>


이하 생략

clone_board.html 설명

  1. 리스트 페이지의 작성글 인덱스,내용,작성 일시를 출력
  2. 글쓰기 버튼 추가

clone_board_add.html 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
생략
<div class="container my-3">
<form method="post" class="post-form">
{{ form.csrf_token }}
{% include "checklist_form_errors.html" %}
Title:&nbsp
<input type="content_title" class="form-control" name="content_title" id="content_title">

<form action="{{ url_for('clone_board.add') }}" method="post">

<label for="content_text" style="font-size:16px;" >Content:</label>
<textarea name="content_text" id="content_text" rows="15"></textarea>
<input type="submit" style="font-size:16px;" value="등록">

</form>
</form>
생략

clone_board_add.html 설명

  1. 에러 출력 관련 처리 위한 코드 추가
  2. 제목과 내용 입력란 추가
  3. 등록 버튼을 누르면 forms 정보와 함께 clone_board add 메소드로 이동

향후 목표

  1. 기능 손보기
    1. 부트스트랩 변경(디자인 수정)
  2. 글 수정기능
    1. 부가적으로 패스워드 필요
  3. 글 삭제
    1. 부가적으로 패스워드 필요
  4. 사진 업로드 기능(글쓰기)
  5. 작성자 이름 추가(닉네임)

Pet-Project-web-게시글 추가 기능 및 날짜 추가

https://praisebak.github.io/2021/06/09/2021-06/web-add-clone-add-board/

Author

Praisebak

Posted on

2021-06-09

Updated on

2021-06-08

Licensed under

Comments