flask 트리 구성

url 셋팅 및 테스트
This commit is contained in:
김민구
2024-01-05 18:23:09 +09:00
parent 4daef569c5
commit 5f9592ac0b
8 changed files with 157 additions and 46 deletions

2
.gitignore vendored
View File

@@ -165,4 +165,6 @@ cython_debug/
.AppleDouble .AppleDouble
.LSOverride .LSOverride
### 제외 파일 ###
*.ini *.ini
*.ipynb

108
app.py
View File

@@ -1,51 +1,71 @@
# -*- coding:utf-8 -*- from flask import Flask, request, render_template, redirect, url_for
import configparser
from flask import Flask
app = Flask(__name__) app = Flask(__name__)
@app.route('/') @app.route('/')
def hello(): def hello():
return result return 'Hello, World!'
@app.route('/hello')
def hellohtml():
return render_template("hello.html")
@app.route('/hellos/<name>')
def hellos(name):
return "hellos {}".format(name)
@app.route('/input/<int:num>')
def input(num):
name = ''
if num == 1:
name = '도라에몽'
elif num == 2:
name = '진구'
elif num == 3:
name = '퉁퉁이'
return "hello {}".format(name)
app.route('/test')
def test1():
return 'test1'
@app.route('/test/')
def test2():
return 'test2'
@app.route('/naver')
def naver():
return render_template("naver.html")
@app.route('/kakao')
def daum():
return redirect("https://www.daum.net/")
@app.route('/urltest')
def url_test():
return redirect(url_for('daum'))
@app.route('/dora')
def myimage():
return render_template("myimage.html")
@app.route('/form')
def formhtml():
return render_template("form.html")
@app.route('/method', methods=['GET', 'POST'])
def method():
if request.method == 'GET':
# args_dict = request.args.to_dict()
# print(args_dict)
num = request.args["num"]
name = request.args.get("name")
return "GET으로 전달된 데이터({}, {})".format(num, name)
else:
num = request.form["num"]
name = request.form["name"]
return "POST로 전달된 데이터({}, {})".format(num, name)
if __name__ == '__main__': if __name__ == '__main__':
with app.test_request_context():
print(url_for('daum'))
app.run() app.run()
import pymysql
config = configparser.ConfigParser()
# DB 연결 설정 정보 세팅 ( db_config.ini에 정의 )
config.read(os.getcwd() + os.sep + 'db_config.ini', encoding='utf-8')
conn = pymysql.connect(
host = config.get('DB_CONFIG', 'HOST'),
port = int(config['DB_CONFIG']['PORT']),
user= config['DB_CONFIG']['USER'],
passwd= config.get('DB_CONFIG', 'PASSWD'),
db= config['DB_CONFIG']['DBNAME'],
charset="utf8")
# Tuple
cur = conn.cursor()
# DictCursor
cur = conn.cursor(pymysql.cursors.DictCursor)
sql = f"""SELECT * FROM authority
"""
# sql = f"""SELECT * FROM product
# """
#cur.execute("set name utf8")
cur.execute(sql)
# 데이터 접근
result = cur.fetchall()
# 연결 종료
conn.close()
print(result)

51
app2.py Normal file
View File

@@ -0,0 +1,51 @@
# -*- coding:utf-8 -*-
import configparser
import os
from flask import Flask
import pymysql
config = configparser.ConfigParser()
# DB 연결 설정 정보 세팅 ( db_config.ini에 정의 )
config.read(os.getcwd() + os.sep + 'db_config.ini', encoding='utf-8')
conn = pymysql.connect(
host = config.get('DB_CONFIG', 'HOST'),
port = int(config['DB_CONFIG']['PORT']),
user= config['DB_CONFIG']['USER'],
passwd= config.get('DB_CONFIG', 'PASSWD'),
db= config['DB_CONFIG']['DBNAME'],
charset="utf8")
# Tuple
cur = conn.cursor()
# DictCursor
cur = conn.cursor(pymysql.cursors.DictCursor)
sql = f"""SELECT * FROM authority
"""
# sql = f"""SELECT * FROM product
# """
#cur.execute("set name utf8")
cur.execute(sql)
# 데이터 접근
result = cur.fetchall()
# 연결 종료
conn.close()
print(result)
app = Flask(__name__)
@app.route('/')
def hello():
return result
if __name__ == '__main__':
app.run()

BIN
static/img/dora.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 KiB

16
templates/form.html Normal file
View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<body>
<h2>WMS</h2>
<form action="/method" method="POST">
<label for="fname">사번:</label><br>
<input type="text" id="fname" name="num"><br>
<label for="lname">이름:</label><br>
<input type="text" id="lname" name="name"><br><br>
<input type="submit" value="확인">
</form>
</body>
</html>

1
templates/hello.html Normal file
View File

@@ -0,0 +1 @@
hello no1

13
templates/myimage.html Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<body>
<h2>도라에몽</h2>
<a href="/hellos/도라에몽">
<img src="static/img/dora.png">
</a>
<a href={{ url_for('hellos', name='도라에몽') }}>
<img src={{ url_for('static', filename='img/dora.png') }}>
</a>
</body>
</html>

8
templates/naver.html Normal file
View File

@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<body>
<script>
location.href="https://www.naver.com/";
</script>
</body>
</html>