点赞功能

CYW
zheyu 1 year ago
parent 937b95a50f
commit f65e6da164

@ -1,63 +1,26 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>菜品列表</title> <title>Main Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head> </head>
<body> <body>
{% for dish in dishes %} {% for dish in dishes %}
<div> <h2>{{ dish.DishName }}</h2>
<h3>{{ dish.DishName }}</h3> <p>收藏数: {{ dish.CollectNumber }}</p>
<p>点赞数: <span id="likeNumber_{{ dish.DishID }}">{{ dish.LikeNumber }}</span></p>
<a href="{{ dish.WebLink }}">查看详情</a> <a href="{{ dish.WebLink }}">查看详情</a>
<button class="like-btn" data-dish="{{ dish.DishName }}">点赞</button> <button onclick="likeDish({{ dish.DishID }})">点赞</button>
<button class="collect-btn" data-dish="{{ dish.DishName }}">收藏</button>
<p>点赞数:<span class="like-number">{{ dish.LikeNumber }}</span></p>
<p>收藏数:<span class="collect-number">{{ dish.CollectNumber }}</span></p>
</div>
{% endfor %} {% endfor %}
<button id="refresh-btn">刷新</button>
<script> <script>
var page = 1; // 当前页数 function likeDish(dishId) {
fetch(`/like_dish/${dishId}/`)
// 加载菜品数据 .then(response => response.json())
function loadDishes() { .then(data => {
$.ajax({ const likeNumberElement = document.getElementById(`likeNumber_${dishId}`);
url: '/mainpage/', likeNumberElement.textContent = data.likeNumber;
type: 'GET', });
data: { 'page': page },
success: function(response) {
// 清空原有数据
$('div').remove();
// 添加新的菜品数据
for (var i = 0; i < response.dishes.length; i++) {
var dish = response.dishes[i];
var dishHtml = '<div>' +
'<h3>' + dish.DishName + '</h3>' +
'<a href="' + dish.WebLink + '">查看详情</a>' +
'<button class="like-btn" data-dish="' + dish.DishName + '">点赞</button>' +
'<button class="collect-btn" data-dish="' + dish.DishName + '">收藏</button>' +
'<p>点赞数:<span class="like-number">' + dish.LikeNumber + '</span></p>' +
'<p>收藏数:<span class="collect-number">' + dish.CollectNumber + '</span></p>' +
'</div>';
$('body').append(dishHtml);
}
}
});
} }
// 刷新按钮点击事件
$('#refresh-btn').click(function() {
page++; // 增加页数
loadDishes();
});
// 页面加载完成后加载第一页菜品数据
$(document).ready(function() {
loadDishes();
});
</script> </script>
</body> </body>
</html> </html>

@ -9,14 +9,16 @@ from .models import MainDishList
from pymysql import Connection from pymysql import Connection
# Create your views here. # Create your views here.
def index(request): def index(request):
return render(request,"mainpage.html") return render(request, "mainpage.html")
def login(request): def login(request):
if request.method =="GET": if request.method == "GET":
return render(request,"login.html") return render(request, "login.html")
elif request.method == "POST": elif request.method == "POST":
post_data = request.POST post_data = request.POST
print("输出从前端获取的用户名和密码") print("输出从前端获取的用户名和密码")
@ -24,7 +26,8 @@ def login(request):
pwd = post_data.get("pwd") pwd = post_data.get("pwd")
print(username) print(username)
print(pwd) print(pwd)
return login_user(username,pwd) return login_user(username, pwd)
def register(request): def register(request):
cnx = Connection( cnx = Connection(
@ -55,7 +58,7 @@ def register(request):
return render(request, 'register.html', {'message': '注册用户时发生错误: {}'.format(err)}) return render(request, 'register.html', {'message': '注册用户时发生错误: {}'.format(err)})
finally: finally:
cursor.close() cursor.close()
cnx.close() # 关闭数据库连接 cnx.close() # 关闭数据库连接
return render(request, 'register.html') return render(request, 'register.html')
@ -86,52 +89,77 @@ def login_user(username, password):
cnx.close() cnx.close()
def get_database_connection(): # def get_database_connection():
return connection # return connection
#
def mainpage(request): #
connection = get_database_connection() # 获取数据库连接 # def get_dishes(page, per_page):
page = int(request.GET.get('page', 1)) # 获取当前页数默认为1 # connection = get_database_connection()
per_page = 10 # 每页显示的数据数量 # offset = (page - 1) * per_page
#
offset = (page - 1) * per_page # 计算偏移量 # with connection.cursor() as cursor:
with connection.cursor() as cursor: # cursor.execute(
cursor.execute( # f"SELECT DishName, WebLink, LikeNumber, CollectNumber FROM MainDishList LIMIT {per_page} OFFSET {offset}")
f"SELECT DishName, WebLink, LikeNumber, CollectNumber FROM MainDishList LIMIT {per_page} OFFSET {offset}") # rows = cursor.fetchall()
rows = cursor.fetchall() #
# dishes = []
dishes = [] # for row in rows:
for row in rows: # dish = {
dish = { # 'DishName': row[0],
'DishName': row[0], # 'WebLink': row[1],
'WebLink': row[1], # 'LikeNumber': row[2],
'LikeNumber': row[2], # 'CollectNumber': row[3]
'CollectNumber': row[3] # }
} # dishes.append(dish)
dishes.append(dish) #
# return dishes
#
#
# def mainpage(request):
# page = int(request.GET.get('page', 1))
# per_page = 10
#
# dishes = get_dishes(page, per_page)
#
# return render(request, 'mainpage.html', {'dishes': dishes})
#
#
# def load_more_dishes(request):
# page = int(request.GET.get('page', 1))
# per_page = 10
#
# dishes = get_dishes(page, per_page)
#
# return JsonResponse({'dishes': dishes})
#
#
# def like_dish(request):
# connection = get_database_connection()
# if request.method == 'POST':
# dish_name = request.POST.get('dish')
# with connection.cursor() as cursor:
# cursor.execute("UPDATE MainDishList SET LikeNumber = LikeNumber + 1 WHERE DishName = %s", [dish_name])
# cursor.execute("SELECT LikeNumber FROM MainDishList WHERE DishName = %s", [dish_name])
# updated_like_number = cursor.fetchone()[0]
# return JsonResponse({'likeNumber': updated_like_number})
#
#
# def collect_dish(request):
# connection = get_database_connection()
# if request.method == 'POST':
# dish_name = request.POST.get('dish')
# with connection.cursor() as cursor:
# cursor.execute("UPDATE MainDishList SET CollectNumber = CollectNumber + 1 WHERE DishName = %s", [dish_name])
# cursor.execute("SELECT CollectNumber FROM MainDishList WHERE DishName = %s", [dish_name])
# updated_collect_number = cursor.fetchone()[0]
# return JsonResponse({'collectNumber': updated_collect_number})
def main_page(request):
dishes = MainDishList.objects.all()
return render(request, 'mainpage.html', {'dishes': dishes}) return render(request, 'mainpage.html', {'dishes': dishes})
def like_dish(request):
connection = get_database_connection() # 获取数据库连接
if request.method == 'POST':
dish_name = request.POST.get('dish')
with connection.cursor() as cursor:
# 更新LikeNumber字段
cursor.execute("UPDATE MainDishList SET LikeNumber = LikeNumber + 1 WHERE DishName = %s", [dish_name])
# 获取更新后的点赞数
cursor.execute("SELECT LikeNumber FROM MainDishList WHERE DishName = %s", [dish_name])
updated_like_number = cursor.fetchone()[0]
return JsonResponse({'likeNumber': updated_like_number})
def collect_dish(request): def like_dish(request, dish_id):
connection = get_database_connection() # 获取数据库连接 dish = MainDishList.objects.get(DishID=dish_id)
if request.method == 'POST': dish.LikeNumber += 1
dish_name = request.POST.get('dish') dish.save()
with connection.cursor() as cursor: return JsonResponse({'likeNumber': dish.LikeNumber})
# 更新CollectNumber字段
cursor.execute("UPDATE MainDishList SET CollectNumber = CollectNumber + 1 WHERE DishName = %s", [dish_name])
# 获取更新后的收藏数
cursor.execute("SELECT CollectNumber FROM MainDishList WHERE DishName = %s", [dish_name])
updated_collect_number = cursor.fetchone()[0]
return JsonResponse({'collectNumber': updated_collect_number})

Loading…
Cancel
Save