본문 바로가기

Python

[DJANGO] MARIA DB 연결(3) - DELETE

dao_emp.py

import pymysql
class DaoEmp:
    def __init__(self):
        self.conn = pymysql.connect(host='127.0.0.1', user='root', password='python',
                           db='python',port=3304, charset='utf8')
        self.curs = self.conn.cursor(pymysql.cursors.DictCursor)
        
    def selectList(self):
        sql = """
        select 
        E_ID, E_NAME, GEN, ADDR 
        from emp
        ORDER BY CAST(E_ID AS INTEGER)
        """
        self.curs.execute(sql)
        list = self.curs.fetchall()
        return list

    def selectOne(self,e_id):
        sql = f"""
        select
            E_ID, E_NAME, GEN, ADDR
        FROM EMP
        WHERE
            E_ID = '{e_id}'
        """
        self.curs.execute(sql)
        emp = self.curs.fetchall()
        return emp[0]
    
    def update(self,e_id,e_name,gen,addr):
        sql = f"""
        update emp
        set
            e_name = '{e_name}',
            gen = '{gen}',
            addr = '{addr}'
        where
            e_id = '{e_id}'
        """
        cnt = self.curs.execute(sql)
        self.conn.commit()
        return cnt
    
    def delete(self,e_id):
        sql = f"""
        delete from emp
        where
            e_id = '{e_id}'
        """
        cnt = self.curs.execute(sql)
        self.conn.commit()
        return cnt
    
    def __del__(self):
        self.curs.close()
        self.conn.close()

urls.py

from django.contrib import admin
from django.urls import path
from HELLO_EMP import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.emp_list),
    path('emp_list',views.emp_list),
    path('emp_detail',views.emp_detail),
    path('emp_mod',views.emp_mod),
    path('emp_mod_act',views.emp_mod_act),
    path('emp_del',views.emp_del),
]

views.py

from django.shortcuts import render
import pymysql
from HELLO_EMP.dao_emp import DaoEmp
from django.views.decorators.csrf import csrf_exempt

def emp_list(request):
    de = DaoEmp()
    emps = de.selectList()
    return render(request, 'emp_list.html', {'emps' : emps })

def emp_detail(request):
    de = DaoEmp()
    e_id = request.GET.get('e_id')
    emp = de.selectOne(e_id)
    return render(request, 'emp_detail.html', {'emp' : emp})

def emp_mod(request):
    de = DaoEmp()
    e_id = request.GET.get('e_id')
    emp = de.selectOne(e_id)
    return render(request, 'emp_mod.html', {'emp' : emp})

@csrf_exempt
def emp_mod_act(request):
    de = DaoEmp()
    e_id = request.POST.get('e_id')
    e_name = request.POST.get('e_name')
    gen = request.POST.get('gen')
    addr = request.POST.get('addr')
    
    cnt = de.update(e_id, e_name, gen, addr)
    
    return render(request, "emp_mod_act.html",{'cnt' : cnt})
        
        
def emp_del(request):
    de = DaoEmp()
    e_id = request.GET.get('e_id')
    cnt = de.delete(e_id)
    
    return render(request, "emp_del_act.html", {'cnt' : cnt})

emp_detail.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function fn_mod(){
	location.href = "emp_mod?e_id={{emp.E_ID}}";
}

function fn_del(){
	location.href = "emp_del?e_id={{emp.E_ID}}";
}
</script>
</head>
<body>
EMP_DETAIL
<form name="frm" action="emp_del_act" method="post">
	<input type="hidden" name="e_id" value="{{emp.E_ID}}"/>
</form>
<table border="1">
	<tr>
		<th>사번</th>
		<td>{{emp.E_ID}}</td>
	</tr>
	<tr>
		<th>이름</th>
		<td>{{emp.E_NAME}}</td>
	</tr>
	<tr>
		<th>성별</th>
		<td>{{emp.GEN}}</td>
	</tr>
	<tr>
		<th>주소</th>
		<td>{{emp.ADDR}}</td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="button" value="수정" onclick= "fn_mod()"/>
			<input type="button" value="삭제" onclick= "fn_del()"/>
		</td>
	</tr>
	
	
</body>
</html>

 

emp_del_act.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>

	let cnt = "{{cnt}}";
	if(cnt!="0"){
		alert("삭제 성공");
		location.href = "emp_list"
	} else{
		alert('삭제 실패');
		history.back();
	}
	
</script>
</head>
<body>

</body>
</html>

실행화면1 - emp_detail

실행화면2 - emp_del_act

 

실행화면3 - emp_list