我是新手。 我正在创建餐厅订单管理系统,一旦客户订购了食物,厨房一侧就可以检索客户的订单并更改他们的食物状态(例如:煮熟或准备送餐)。
我想使用选择选项来编辑客户订单的状态,当我单击“更改状态”按钮时将其保存到数据库中,并显示在kitchen_page.html页面中。但是我不确定该怎么做?
有人可以帮助我吗?
views.py:
def kitchen_view(request):
chef_view = OrderItem.objects.all()
#chef_view = OrderItem.objects.get(OrderId=OrderId)
return render(request, 'restaurants/kitchen_page.html', {'chef_view': chef_view})
if request.method == "POST":
OrderId = request.POST.get("OrderId")
Status = request.POST.get("Status")
chef_view.OrderId = OrderId
chef_view.Status = Status
get_status = chef_view.save()
return render(request, 'restaurants/kitchen_page.html', {'chef_view':chef_view})
答案 0 :(得分:0)
您要在发生任何事情之前返回渲染。
def kitchen_view(request):
chef_view = OrderItem.objects.all()
# set the status if request method is post
if request.method == "POST":
order_id = request.POST.get("OrderId")
status = request.POST.get("Status")
# get order instance by id and change the status
order = OrderItem.objects.get(pk=order_id)
order.status = status
order.save()
# since the render data is the same, you can just call it once.
return render(request, 'restaurants/kitchen_page.html', {'chef_view':chef_view})