Python - Flask(session) / 로그인, 회원정보 수정, 로그아웃, 회원리스트 받아오기
Flask 로그인 및 세션 생성
- 쿠키(cookie)는 클라이언트 피씨에 생성
- 세선은 서버 메모리에 생성되어 생성 후 바로 사용 가능
sqlite3 db 및 table생성
-
DB 서버 필요 없는 파일 기반 Embedded SQL DB 엔진
-
sqlite브라우저 : http://sqlitebrowser.org/
-
- db 없을 시 python.db 자동 생성 됨
- CREAT TABLE
- INSERT
<로그인>
<회원정보 수정>
<로그아웃>
<회원정보 리스트 가져오기>
Flask 리스트 페이징 하기(list paging)
- SQLite 사용
- Table, 데이터 생성 : http://thecoding.kr/category/phython/python-sqlite/
- template 폴더에 miniboard 생성
- lists.html, list.html 생성
- 데이터베이스에 리스트 db 생성
- 반복구문으로 가져오기 (html)
- 회원정보 리스트 페이징 하기
<리스트 가져오기 & 페이징 하기 플라스크 소스코드>
from flask import Flask, request, session, render_template, url_for
import sqlite3
import math
app = Flask(__name__)
@app.route('/') # 처음에 실행시키는 page
def index():
return "root"
# 리스트 가져오기
def select():
conn = sqlite3.connect('python.db')
cursor = conn.cursor()
sql = 'select * from miniboard order by idx desc'
cursor.execute(sql)
rows = cursor.fetchall()
conn.close()
return rows
def list_test():
list = select()
print(list)
# 페이징
def select_count():
conn = sqlite3.connect('python.db')
cursor = conn.cursor()
sql = 'select count(idx) from miniboard'
cursor.execute(sql)
row = cursor.fetchone()
cursor.close()
conn.close()
return row[0] # 튜플형이므로 인덱스 줘서 값 받아야 함
def select_page(list_limit, page):
conn = sqlite3.connect('python.db')
cursor = conn.cursor()
offset = (page -1) * list_limit
sql = 'select * from miniboard order by idx desc limit ? offset ?'
cursor.execute(sql, (list_limit, offset,)) # 물음표 2개 받아주기
row = cursor.fetchall()
cursor.close()
conn.close()
return row
# 리스트 가져오기
@app.route('/lists')
def lists():
lists = select()
return render_template('miniboard/lists.html', lists=lists)
# 페이징
@app.route('/list/<int:page>')
def list(page):
list_num = 5
list_count = select_count()
# page_count = int(list_count / list_num)
page_count = math.ceil(list_count/list_num)
list = select_page(list_num, page)
return render_template('miniboard/list.html', list=list, page_count=page_count)
if __name__ == '__main__':
app.secret_key = '20200601'
app.run(debug=True)
# list_test() list 잘 불러오는지 테스트용