메인화면~
회원가입~
회원가입 목록
회원 수정및업데이트
삭제기능
views.py
from flask import Blueprint, render_template, request, redirect, url_for
from members import MyMemberDAO
members_bp = Blueprint('members', __name__)
# 등록 페이지
@members_bp.route('/register', methods=['GET', 'POST'], endpoint='register')
def register():
if request.method == 'POST':
# 회원 가입 폼에서 전송된 데이터 처리
name = request.form['username']
id = request.form['id']
phone = request.form['phone']
# MyMemberDAO를 사용하여 데이터베이스에 사용자 정보 저장
dao = MyMemberDAO()
dao.create_table()
dao.insert_data((name, id, phone))
# 회원가입이 성공하면 '/members' 페이지로 리디렉션
return redirect(url_for('members.list_members')) # 회원 목록 페이지로 리디렉션
return render_template('members/register.html') # 회원 가입 페이지 렌더링
# 회원 목록 페이지
@members_bp.route('/members', methods=['GET'], endpoint='list_members')
def list_members():
dao = MyMemberDAO()
data = dao.read_all_data()
return render_template('members/list.html', data=data)
@members_bp.route('/delete/<int:id>', methods=['GET'], endpoint='delete_member')
def delete_member(id):
dao = MyMemberDAO()
# 사용자 정보를 삭제
dao.delete_data(id)
# 사용자를 삭제한 후, 회원 목록 페이지로 리디렉션
return redirect(url_for('members.list_members'))
@members_bp.route('/save_member', methods=['POST'], endpoint='save_member')
def save_member():
# 'members.save_member' 엔드포인트에 대한 작업 수행
# ...
return redirect(url_for('members.list_members'))
# Flask 뷰 함수에서 member 객체 생성 및 전달
@members_bp.route('/edit/<int:id>', methods=['GET', 'POST'], endpoint='edit_member')
def edit_member(id):
dao = MyMemberDAO()
if request.method == 'POST':
# 폼에서 전송된 데이터를 가져옴
username = request.form['username']
phone = request.form['phone']
# 사용자 정보 업데이트
dao.update_data(id, (username, phone))
# 업데이트 후, 사용자 정보 페이지로 리디렉션
return redirect(url_for('members.list_members'))
# POST 요청이 아닌 경우, 회원 정보 수정 페이지 렌더링
user_data = dao.read_data_by_id(id)
member = {
'no': user_data[0],
'id': user_data[1],
'name': user_data[2],
'phone': user_data[3]
}
return render_template('members/edit.html', member=member)
MyMemberDAO.py
import sqlite3
class MyMemberDAO:
def __init__(self):
self.conn = sqlite3.connect('my_database.db')
def create_table(self):
cursor = self.conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS mymember (
no INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
id TEXT,
phone TEXT
);''')
self.conn.commit()
def insert_data(self, data):
cursor = self.conn.cursor()
cursor.execute('''INSERT INTO mymember (name, id, phone)
VALUES (?, ?, ?);''', data)
self.conn.commit()
def read_all_data(self):
cursor = self.conn.cursor()
cursor.execute('''SELECT * FROM mymember;''')
data = cursor.fetchall()
return data
def update_data(self, id, new_data):
cursor = self.conn.cursor()
cursor.execute('''UPDATE mymember
SET name=?, phone=?
WHERE no=?;''', (*new_data, id))
self.conn.commit()
def delete_data(self, no):
cursor = self.conn.cursor()
cursor.execute('''DELETE FROM mymember WHERE no=?;''', (no,))
self.conn.commit()
def read_data_by_id(self, id):
cursor = self.conn.cursor()
cursor.execute("SELECT * FROM mymember WHERE no=?", (id,))
data = cursor.fetchone()
return data
run.py
from flask import Flask, render_template, request, redirect, url_for, Blueprint
import sqlite3
from members.views import members_bp
from gugudan.views import gugudan_bp
from calculator.views import calculator_bp
from members import MyMemberDAO
app = Flask(__name__, template_folder='templates')
# 데이터베이스 연결 함수
def get_data_from_database():
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM mymember")
data = cursor.fetchall()
conn.close()
return data
@app.route('/')
def menu():
return render_template('main.html')
@app.route('/edit/<int:id>', methods=['GET', 'POST'], endpoint='edit_member')
def edit_member(id):
dao = MyMemberDAO()
user_data = dao.read_data_by_id(id)
member = {
'no': user_data[0],
'id': user_data[1],
'name': user_data[2],
'phone': user_data[3]
}
return render_template('members/edit.html', member=member)
def list_members():
data = get_data_from_database()
return render_template('members/list.html', data=data)
# Blueprint 등록
app.register_blueprint(members_bp, url_prefix='/members')
app.register_blueprint(gugudan_bp, url_prefix='/gugudan')
app.register_blueprint(calculator_bp, url_prefix='/calculator')
if __name__ == '__main__':
app.run(debug=True)
너무길어져서 html파일은 안올리겠습니다~