quilt code

[python] flask 본문

daily/파이썬

[python] flask

김뱅쇼 2023. 4. 24. 21:06

1. my_flask




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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request
from flask.templating import render_template
from day08.dao_book import DaoBook
import json
app = Flask(__name__)
 
db = DaoBook()
 
 
 
@app.route('/')
@app.route('/book_list')
def book_list():
    list = db.selectList()
    return render_template('book_list.html'
 
@app.route('/book_select_list.ajax', methods =['POST'])
def ajax_book_select_list():
    list = db.selectList()
    jsondata=json.dumps(list)   
    
    return jsondata
 
@app.route("/book_select.ajax", methods =['GET'])
def ajax_book_detail():
    b_id = request.args.get('b_id')
    book = db.select(b_id)
    jsondata=json.dumps(book)  
 
    return jsondata
 
@app.route('/book_add.ajax',methods=['POST'])
def book_add():
    b_nm = request.form['b_nm']
    author = request.form['author']
    publisher = request.form['publisher']
    
    cnt = db.insert(b_nm,author,publisher)
    jsondata=json.dumps(cnt)
 
    return jsondata
 
 
@app.route('/book_mod.ajax',methods=['POST'])
def book_mod():
    b_id = request.form['b_id']
    b_nm = request.form['b_nm']
    author = request.form['author']
    publisher = request.form['publisher']
    
    cnt = db.insert(b_nm,author,publisher)
    jsondata=json.dumps(cnt)
 
    return jsondata
 
@app.route('/book_del.ajax',methods=['POST'])
def book_del():
    b_id = request.form['b_id']
    
    cnt = db.delete(b_id)
    jsondata=json.dumps(cnt)
 
    return jsondata
 
 
if __name__ == '__main__':
    app.run(debug=True)
    
    
    
    
    
    
cs


 
 

2. book_list.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="static/jquery-3.6.4.js"></script>
<script type="text/javascript">
function fn_ajax(){
    $.ajax({
        type : 'POST',                                 
        url : 'book_select_list.ajax',
        data : JSON.stringify(),
        dataType : 'JSON',
        
        success : function(resp) {
            var list=resp;
            var html = "";
            
            for(let i = 0; i<list.length; i++){
                var b_id = list[i].b_id;
                var b_nm = list[i].b_nm;
                var author = list[i].author;
                var publisher = list[i].publisher;
                
                html += `<tr>`;
                html += `    <td><a href="javascript:fn_detail('${b_id}')">${b_id}</a></td>`;
                html += `    <td>${b_nm}</td>`;
                html += `    <td>${author}</td>`;
                html += `    <td>${publisher}</td>`;
                html += `</tr>`;
                            
            }
            var objtbody = document.querySelector("#my_tbody");
            objtbody.innerHTML = html;
        },
        error : function() {
            alert('error');
        }
    });     
}
 
function fn_detail(b_id){
    var obj_b_id = document.querySelector("#b_id");
    var obj_b_nm = document.querySelector("#b_nm");
    var obj_author = document.querySelector("#author");
    var obj_publisher = document.querySelector("#publisher");
    
    $.ajax({
        url : 'book_select.ajax',  
        type : "get",
        contentType : 'application/json; charset=utf-8',
        data : { b_id : b_id },
        dataType : "json",
        success : function(resp) {
            var book = resp;
            console.log(book.b_id);
            $("#b_id").val(book.b_id),
            $("#b_nm").val(book.b_nm),
            $("#author").val(book.author),
            $("#publisher").val(book.publisher)
            
        },
        error : function() {
            alert('error');
        }
    });
}
 
function fn_add(){
 
    $.ajax({
        url : 'book_add.ajax',  
        type : "post",
        data : {
            b_id : $("#b_id").val(),
            b_nm : $("#b_nm").val(),
            author : $("#author").val(),
            publisher : $("#publisher").val()
             
        },
        dataType : "json",
        success : function(resp) {
            console.log(resp);
            var cnt = resp;
            if(cnt == 1){
                fn_ajax(); 
                alert("정상적으로 추가되었습니다.");
                $("#b_nm").val("");
                $("#author").val("");
                $("#publisher").val("");
                
            } else {
                alert("추가 중 문제가 생겼습니다.");
            }
        },
        error : function() {
            alert('error');
        }
    });
}
 
 
function fn_mod(){
 
    $.ajax({
        url : 'book_mod.ajax',  
        type : "post",
        data : {
            b_id : $("#b_id").val(),
            b_nm : $("#b_nm").val(),
            author : $("#author").val(),
            publisher : $("#publisher").val()
             
        },
        dataType : "json",
        success : function(resp) {
            console.log(resp);
            var cnt = resp;
            if(cnt == 1){
                fn_ajax(); 
                alert("정상적으로 수정되었습니다.");
                $("#b_nm").val("");
                $("#author").val("");
                $("#publisher").val("");
                
            } else {
                alert("수정 중 문제가 생겼습니다.");
            }
        },
        error : function() {
            alert('error');
        }
    });
}
 
function fn_del(){
 
    $.ajax({
        url : 'book_del.ajax',  
        type : "post",
        data : {
            b_id : $("#b_id").val(),
             
        },
        dataType : "json",
        success : function(resp) {
            console.log(resp);
            var cnt = resp;
            if(cnt == 1){
                fn_ajax(); 
                alert("정상적으로 삭제되었습니다.");
                $("#b_nm").val("");
                $("#author").val("");
                $("#publisher").val("");
                
            } else {
                alert("삭제 중 문제가 생겼습니다.");
            }
        },
        error : function() {
            alert('error');
        }
    });
}
 
$(document).ready(function(){
    fn_ajax();
});
</script>
</head>
<body>
 
book_list <br />
<a href="javascript:fn_ajax()">ajax</a>
 
<table border="1px">
    <tr>
        <td>책번호</td>
        <td>책이름</td>
        <td>작가</td>
        <td>출판사</td>
    </tr>
    <tbody id="my_tbody">
    
    </tbody>
</table>
 
<table border="1px">
    <tr>
        <td>책번호</td>
        <td>
            <input type="text" id="b_id" />
        </td>
    </tr>
    <tr>
        <td>책이름</td>
        <td>
            <input type="text" id="b_nm" />
        </td>
    </tr>
    <tr>
        <td>작가</td>
        <td>
            <input type="text" id="author" />
        </td>
    </tr>
    <tr>
        <td>출판사</td>
        <td>
            <input type="text" id="publisher" />
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" value="추가" onclick="fn_add()" />
            <input type="button" value="수정" onclick="fn_mod()" />
            <input type="button" value="삭제" onclick="fn_del()" />
        </td>
    </tr>
    
</table>
</body>
</html>
cs
 

 

'daily > 파이썬' 카테고리의 다른 글

[python] crawl - selenium  (0) 2023.04.25
[iBatis] iBatis (2)  (0) 2023.03.21
[iBatis] iBatis (1)  (0) 2023.03.21
[파이썬] Day02  (0) 2023.03.15
[파이썬] Day01  (0) 2023.03.13