Python/python 클론코딩
인스타그램 클론코딩 (포스팅 수정, 삭제)
SeongWon
2020. 4. 11. 13:35
반응형
1. 포스팅 수정
<포스팅 디테일 페이지>
상단 우측에 Modal 팝업을 만들어서 수정 및 삭제 작업을 할 수 있도록 구축하였다.
Modal Pop-up?
: 버튼이나 사진을 클릭 했을때 튀어나오는 나오는 화면을 modal pop-up 이라고 한다.
쉽게 말해서, 인스타그램이나 페이스 북에서 사진을 클릭 했을 때, 화면 전환 없이 해당 페이지에서 팝업 창이 뜨는 것을 말한다.
- modal pop-up의 특징으로는 페이지가 넘어 가지 않으므로 작은 크기의 팝업 창이 화면 중앙에 위치한다.
<포스팅 수정 페이지>
포스팅을 업로드 했을 당시의 데이터가 입력 칸에 띄워져 있으며, 이를 지우거나 수정하여 입력하면 그 데이터가 DB에 새롭게 입력되어 들어가도록 post_modifyView.py의 코드를 작성하였다.
@login_required
def post_modifyView(request, postmodify):
if request.method == "GET":
user = request.user
cursor = connection.cursor()
strSql = "SELECT post_img_src, content, post_id"
strSql += " FROM post"
strSql += " WHERE post_id = (%s)"
result = cursor.execute(strSql, (postmodify,))
datas = cursor.fetchall()
# 해시태그 가져오기
post_hashtag_sql = "SELECT keyword"
post_hashtag_sql += " FROM hashtag"
post_hashtag_sql += " LEFT OUTER JOIN post_hashtag on hashtag.hashtag_id = post_hashtag.hashtag_id"
post_hashtag_sql += " WHERE post_id = (%s)"
post_hashtag_sql_result = cursor.execute(post_hashtag_sql, (postmodify,))
post_hashtag_sql_datas = cursor.fetchall()
hashtag = []
for i in range(len(post_hashtag_sql_datas)):
hashtag.append(post_hashtag_sql_datas[i][0])
hashtag_result = ' '.join(hashtag)
connection.commit()
connection.close()
post = {'post_img_src': datas[0][0],
'content': datas[0][1],
'post_id': datas[0][2],
'hashtag': hashtag_result}
return render(request, 'post_modify.html', {'post': post})
elif request.method == "POST":
new_content = request.POST.get("content")
new_hashtag = request.POST.get("hashtag")
split_new_hashtag = new_hashtag.split(' ')
cursor = connection.cursor()
# 포스트 content 수정 삽입
strSql = "UPDATE post"
strSql += " SET content = (%s)"
strSql += " WHERE post_id = (%s)"
results = cursor.execute(strSql, (new_content, postmodify,))
datas = cursor.fetchall()
connection.commit()
connection.close()
return redirect('instagram:detail', postmodify)
해시테그에 대한 수정 및 삭제 과정은 나중에 따로 작성하도록 하겠다.
2. 포스트 삭제
위의 디테일 페이지에서 "포스트 삭제" 버튼을 누르면 바로 post_deleteView.py로 넘어간다.
@login_required
def post_deleteView(request, postdelete):
user = request.user
cursor = connection.cursor()
strSql = "DELETE"
strSql += " FROM post"
strSql += " WHERE post_id = (%s)"
result = cursor.execute(strSql, (postdelete,))
connection.commit()
connection.close()
return redirect('instagram:list', user.username)
<포스트 리스트 (유저 정보) 페이지>
삭제 버튼을 누른 후, 해당 포스팅 게시글은 포스트 리스트 페이지에서는 물론, 메인 페이지에서도 사라지게 된다.
반응형