通过flask实现web页面简单的增删改查 # 1.后台程序falsk_web01.py #coding:utf-8 from flask import Flask,render_template,request,redirect import fileutils # 引入file_dict用户列表 fileutils.file_read() app = Flask(__name__) @app.route(‘/‘) def index(): return render_template(‘login.html‘) @app.route(‘/loginaction/‘, methods = ["POST","GET"]) def login(): error_msg = ‘‘ if request.method == ‘GET‘: username = request.args.get(‘username‘) password = request.args.get(‘password‘) else: username = request.form.get(‘username‘) password = request.form.get(‘password‘) print(‘username:%s,password:%s‘ % (username,password)) if username and password: if username == "admin" and password == "admin": return redirect(‘/list‘) else: error_msg = "username or password is wrong" else: error_msg = ‘need username and password‘ return render_template(‘login.html‘, error_msg = error_msg) @app.route(‘/list/‘) def userlist(): userlist = fileutils.file_read().items() print(‘userlist:%s‘ % userlist) return render_template(‘list.html‘, userlist = userlist) @app.route(‘/update/‘) def update(): username = request.args.get(‘username‘) password = fileutils.file_read().get(username) user = [username, password] print(‘update:%s‘ % user) return render_template(‘update.html‘, user = user) @app.route(‘/updateaction/‘, methods = [‘POST‘]) def updateaction(): params = request.args if request.method == ‘GET‘ else request.form username = params.get(‘username‘) password = params.get(‘password‘) fileutils.file_dict[username] = password fileutils.file_write() return redirect(‘/list/‘) @app.route(‘/add/‘) def add(): return render_template(‘add.html‘) @app.route(‘/addaction/‘, methods = [‘POST‘]) def addaction(): params = request.args if request.method == ‘GET‘ else request.form username = params.get(‘username‘) password = params.get(‘password‘) if username in fileutils.file_dict: return redirect(‘/list/‘) else: fileutils.file_dict[username] = password fileutils.file_write() return redirect(‘/list/‘) @app.route(‘/delete/‘) def delete(): username = request.args.get(‘username‘) fileutils.file_dict.pop(username) fileutils.file_write() return redirect(‘/list/‘) if __name__ == "__main__": app.run(host = ‘0.0.0.0‘, debug = True) # 2.工具类fileutils.py # coding:utf-8 file_dict = {} # file => dict def file_read(): with open(‘user.txt‘) as f: for line in f.read().split(‘\n‘): if line: tmp = line.split(‘:‘) file_dict[tmp[0]] = tmp[1] return file_dict # ditc => file def file_write(): file_arr = [] for user,pwd in file_dict.items(): file_arr.append(‘%s:%s‘ % (user, pwd)) print(file_arr) with open(‘user.txt‘, ‘w‘) as f: f.write(‘\n‘.join(file_arr)) if __name__ == "__main__": print(file_read()) file_write() # 3.模板文件templates中的登陆、列表、增删改查页面 ①用户登录页面login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <p style="color:red"> {{error_msg}} </p> <form action=‘/loginaction/‘ method="post"> username: <input type="text" name="username" /> password: <input type="text" name="password" /> <input type="submit" value="login"> </form> </body> </html> ②更新用户页面update.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action=‘/updateaction/‘ method="post"> username:{{user[0]}} <input type="hidden" name="username" value="{{user[0]}}" /> password: <input type="text" name="password" value="{{user[1]}}" /> <input type="submit" value="update"> </form> </body> </html> ③添加用户页面add.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action=‘/addaction/‘ method="post"> username: <input type="text" name="username" /> password: <input type="text" name="password" /> <input type="submit" value="add"> </form> </body> </html> ④列表页面list.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <table border="1" cellpadding="0" cellspacing="0"> <tr> <td>user</td> <td>pwd</td> <td>action</td> </tr> {% for user in userlist %} <tr> <td>{{user[0]}}</td> <td>{{user[1]}}</td> <td> <a href="/delete/?username={{user[0]}}">delete</a> <a href="/update/?username={{user[0]}}">update</a> <a href="/add/">add</a> </td> </tr> {% endfor %} </table> </body> </html> 4.用户信息文件 user.txt tom:123 jack:123 user2:000 user1:pwd1
原文地址:https://www.cnblogs.com/reblue520/p/8467605.html
时间: 2024-10-10 20:20:38