我在会话中存储和对象ID,然后让它查询和显示该模型的对象,但是当我从会话中删除此ID并从数据库中删除该对象时,在重新加载页面后仍会得到该对象这不是我想要的,只有在重新启动服务器后此对象才会消失,请参见代码
this is where i am deleting the objects from the db and deleting the session
def reset(request):
try:
id = request.session.get('id')
if id:
Item.objects.filter(order=id).all().delete()
Order.objects.get(pk=id).delete()
del request.session['id']
print('Id have been deleted ', request.session['id'])
return JsonResponse('The current order has been delete', safe=False)
else:
print('There\'s no order')
return JsonResponse('There was no order', safe=False)
except Exception as e:
return JsonResponse('An error occured here ' + str(e), safe=False)
This is where i am querying and rendering objects to template after testing if id is in session before querying
@login_required
def dashboard(request):
customer_form = CustomerInfoForm()
form = TransactionForm(initial={'tax':0,'price':0, 'price_per_item':0})
try:
if 'id' in request.session:
id = request.session['id']
print('Id in dashboard is ', id)
order = Order.objects.get(pk=id)
sum_of_price = order.item_set.all().aggregate(Sum('price'))
#Get sum of all item prices.
if sum_of_price :
context['price'] = sum_of_price['price__sum']
context['current_order'] = order.item_set.all()
form = TransactionForm(initial={'tax':0,'price':0, 'price_per_item':0,'contact':order.customer_contact})
context['form'] = form
context['customer_form'] = customer_form
return render(request, 'main/dashboard.html', context)
except Exception as e:
return render(request, 'main/dashboard.html', context)
Thanks