基于python的比赛报名系统设计与实现,使用flask框架以及mysql数据库,主要功能有:注册,登录,个人信息修改,报名,取消报名,评委登录,登录后查看报名选手信息并同意报名,管理员登录后更改公告信息的代码以及数据库以及html设计
时间: 2024-06-08 18:09:12 浏览: 170
这个系统的实现需要以下几个模块:
1. 用户模块:包括注册、登录、修改个人信息等功能。注册时需要填写用户名、密码和邮箱等信息,并进行验证,登录时需要验证用户名和密码是否正确,修改个人信息可以修改用户的基本信息。
2. 报名模块:包括报名和取消报名功能。报名时需要填写比赛名称、参赛选手姓名、联系方式等信息,并进行验证,取消报名可以取消已经报名的比赛。
3. 评委模块:评委登录后可以查看已经报名的选手信息,并进行同意或者拒绝报名的操作。
4. 管理员模块:管理员登录后可以更改公告信息,对数据库进行操作,以及进行html设计等功能。
下面是使用flask框架和mysql数据库实现的基本代码:
1. 用户模块
```python
from flask import Flask, render_template, request, session, redirect, url_for
import pymysql
app = Flask(__name__)
app.secret_key = '123456'
# 连接mysql数据库
db = pymysql.connect(host='localhost', user='root', password='123456', db='competition', charset='utf8')
# 用户注册
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
email = request.form['email']
cursor = db.cursor()
sql = "INSERT INTO users (username, password, email) VALUES (%s, %s, %s)"
try:
cursor.execute(sql, (username, password, email))
db.commit()
return redirect(url_for('login'))
except:
db.rollback()
return render_template('register.html')
return render_template('register.html')
# 用户登录
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cursor = db.cursor()
sql = "SELECT * FROM users WHERE username=%s AND password=%s"
cursor.execute(sql, (username, password))
user = cursor.fetchone()
if user:
session['username'] = user[1]
return redirect(url_for('index'))
else:
return render_template('login.html')
return render_template('login.html')
# 修改个人信息
@app.route('/profile', methods=['GET', 'POST'])
def profile():
if 'username' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
username = session['username']
email = request.form['email']
cursor = db.cursor()
sql = "UPDATE users SET email=%s WHERE username=%s"
cursor.execute(sql, (email, username))
db.commit()
return redirect(url_for('index'))
username = session['username']
cursor = db.cursor()
sql = "SELECT * FROM users WHERE username=%s"
cursor.execute(sql, (username,))
user = cursor.fetchone()
return render_template('profile.html', user=user)
# 退出登录
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
```
2. 报名模块
```python
# 报名
@app.route('/apply', methods=['GET', 'POST'])
def apply():
if 'username' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
username = session['username']
competition = request.form['competition']
name = request.form['name']
phone = request.form['phone']
cursor = db.cursor()
sql = "INSERT INTO applications (competition, name, phone, username) VALUES (%s, %s, %s, %s)"
try:
cursor.execute(sql, (competition, name, phone, username))
db.commit()
return redirect(url_for('index'))
except:
db.rollback()
return render_template('apply.html')
return render_template('apply.html')
# 取消报名
@app.route('/cancel', methods=['GET', 'POST'])
def cancel():
if 'username' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
id = request.form['id']
cursor = db.cursor()
sql = "DELETE FROM applications WHERE id=%s"
cursor.execute(sql, (id,))
db.commit()
return redirect(url_for('index'))
username = session['username']
cursor = db.cursor()
sql = "SELECT * FROM applications WHERE username=%s"
cursor.execute(sql, (username,))
applications = cursor.fetchall()
return render_template('cancel.html', applications=applications)
```
3. 评委模块
```python
# 评委登录
@app.route('/judge_login', methods=['GET', 'POST'])
def judge_login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cursor = db.cursor()
sql = "SELECT * FROM judges WHERE username=%s AND password=%s"
cursor.execute(sql, (username, password))
judge = cursor.fetchone()
if judge:
session['judge'] = judge[1]
return redirect(url_for('judge'))
else:
return render_template('judge_login.html')
return render_template('judge_login.html')
# 查看选手信息
@app.route('/judge', methods=['GET', 'POST'])
def judge():
if 'judge' not in session:
return redirect(url_for('judge_login'))
cursor = db.cursor()
sql = "SELECT * FROM applications"
cursor.execute(sql)
applications = cursor.fetchall()
return render_template('judge.html', applications=applications)
# 同意报名
@app.route('/agree', methods=['GET', 'POST'])
def agree():
if 'judge' not in session:
return redirect(url_for('judge_login'))
if request.method == 'POST':
id = request.form['id']
cursor = db.cursor()
sql = "UPDATE applications SET status=%s WHERE id=%s"
cursor.execute(sql, ('已通过', id))
db.commit()
return redirect(url_for('judge'))
return redirect(url_for('judge'))
```
4. 管理员模块
```python
# 管理员登录
@app.route('/admin_login', methods=['GET', 'POST'])
def admin_login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cursor = db.cursor()
sql = "SELECT * FROM admins WHERE username=%s AND password=%s"
cursor.execute(sql, (username, password))
admin = cursor.fetchone()
if admin:
session['admin'] = admin[1]
return redirect(url_for('admin'))
else:
return render_template('admin_login.html')
return render_template('admin_login.html')
# 更改公告信息
@app.route('/admin', methods=['GET', 'POST'])
def admin():
if 'admin' not in session:
return redirect(url_for('admin_login'))
if request.method == 'POST':
notice = request.form['notice']
cursor = db.cursor()
sql = "UPDATE notices SET content=%s WHERE id=1"
cursor.execute(sql, (notice,))
db.commit()
cursor = db.cursor()
sql = "SELECT * FROM notices WHERE id=1"
cursor.execute(sql)
notice = cursor.fetchone()
return render_template('admin.html', notice=notice[1])
# 对数据库进行操作
@app.route('/sql', methods=['GET', 'POST'])
def sql():
if 'admin' not in session:
return redirect(url_for('admin_login'))
if request.method == 'POST':
sql = request.form['sql']
cursor = db.cursor()
try:
cursor.execute(sql)
data = cursor.fetchall()
db.commit()
return render_template('sql.html', data=data)
except:
db.rollback()
return render_template('sql.html')
return render_template('sql.html')
```
以上是基于python的比赛报名系统设计与实现的基本代码,你可以根据需求进行相应的修改和完善。
阅读全文