java/php/node.js/python城市旅游景点门票订购系统【2024年毕设】
这是一个使用Python Flask框架实现的城市旅游景点门票在线预订系统的代码示例:
from flask import Flask, render_template, request, redirect, url_for, session
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tickets.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True, nullable=False)
password = db.Column(db.String(100), nullable=False)
class Ticket(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
location = db.Column(db.String(200), nullable=False)
price = db.Column(db.Float, nullable=False)
date = db.Column(db.Date, nullable=False)
# 用户登录
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username, password=password).first()
if user:
session['user'] = username
return redirect(url_for('index'))
return 'Login Unsuccessful. Please check your username and password.'
return render_template('login.html')
# 用户注册
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User(username=username, password=password)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html')
# 门票预订
@app.route('/book_ticket/<int:ticket_id>', methods=['POST'])
def book_ticket(ticket_id):
if 'user' not in session:
return redirect(url_for('login'))
ticket = Ticket.query.get(ticket_id)
if ticket:
# 这里应该添加订票逻辑,例如减少库存等
# ...
return 'Ticket booked successfully.'
return 'Ticket not found.'
if __name__ == '__main__':
app.run(debug=True)
这个简易的Python Flask示例展示了如何使用SQLAlchemy和Flask SQLAlchemy来创建一个简单的在线门票预订系统。它包括用户登录和注册功能,以及一个基本的门票预
评论已关闭