用python写一个在线评价系统
时间: 2024-05-14 18:17:52 浏览: 14
好的,以下是一个简单的在线评价系统的Python实现,使用Flask框架和SQLite数据库:
```python
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
app.secret_key = "secret_key"
@app.route('/')
def index():
conn = sqlite3.connect('reviews.db')
c = conn.cursor()
c.execute("SELECT * FROM reviews")
reviews = c.fetchall()
conn.close()
return render_template('index.html', reviews=reviews)
@app.route('/add_review', methods=['GET', 'POST'])
def add_review():
if request.method == 'POST':
name = request.form['name']
rating = request.form['rating']
comment = request.form['comment']
conn = sqlite3.connect('reviews.db')
c = conn.cursor()
c.execute("INSERT INTO reviews (name, rating, comment) VALUES (?, ?, ?)", (name, rating, comment))
conn.commit()
conn.close()
return redirect(url_for('index'))
return render_template('add_review.html')
if __name__ == '__main__':
app.run(debug=True)
```
其中,`index()`函数用于显示所有评论,`add_review()`函数用于添加新评论。评论数据存储在名为`reviews.db`的SQLite数据库中,包含三个列:`name`,`rating`和`comment`。评论展示界面使用HTML模板`index.html`,添加评论界面使用HTML模板`add_review.html`。
你需要自己创建`reviews.db`数据库文件,并创建`reviews`表。可以使用下面的SQL语句创建表:
```sql
CREATE TABLE reviews (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
rating INTEGER NOT NULL,
comment TEXT NOT NULL
);
```
这是一个基本的在线评价系统,你可以根据自己的需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)