Python电影售票系统管理的前后端代码
时间: 2024-06-23 20:02:22 浏览: 102
sqlserver_py-master_python飞机订票系统_
5星 · 资源好评率100%
在Python中构建一个电影售票系统,通常会涉及前后端分离的开发。前端使用HTML、CSS和JavaScript(可能还会结合React或Vue等框架)来创建用户界面,后端则使用Python的Web框架,如Django或Flask,处理业务逻辑和数据交互。
**前端代码示例(假设用React):**
```jsx
// 前端组件 - MovieTicket.js
import React, { useState } from 'react';
import axios from 'axios';
function MovieTicket() {
const [movieName, setMovieName] = useState('');
const [showtimes, setShowtimes] = useState([]);
function selectShowtime(showtime) {
// 发送POST请求到后端获取选座信息
axios.post('/api/tickets', { movieName, showtime })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error selecting showtime:', error);
});
}
return (
<div>
<input type="text" value={movieName} onChange={(e) => setMovieName(e.target.value)} />
<select onChange={(e) => setShowtimes(e.target.value)}>
{/* 显示可用场次 */}
{showtimes.map((showtime, index) => (
<option key={index} value={showtime}>{showtime}</option>
))}
</select>
<button onClick={() => selectShowtime(showtimes)}>选择</button>
</div>
);
}
export default MovieTicket;
```
**后端代码示例(使用Flask):**
```python
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tickets.db'
db = SQLAlchemy(app)
class Showtimes(db.Model):
id = db.Column(db.Integer, primary_key=True)
movie_name = db.Column(db.String(80), nullable=False)
showtime = db.Column(db.String(10), nullable=False)
@app.route('/api/tickets', methods=['POST'])
def buy_ticket():
data = request.get_json()
new_ticket = Showtimes(movie_name=data['movieName'], showtime=data['showtime'])
db.session.add(new_ticket)
db.session.commit()
return jsonify({'message': 'Ticket purchased successfully'}), 201
if __name__ == '__main__':
app.run(debug=True)
```
阅读全文