我有一个函数我的django视图来创建订单,订单的订单号与项目挂钩。
这是views.py
def summary(request): # Client View
context = {}
request_user = request.user
all_items = Cart.objects.filter(client=request_user, ordered=False)
print "ALL ITEM", all_items
client = request.user
# print "Client is ", client
location=request.user.signup.country
print "LOCATION ", location
if request.method == "POST":
if request.POST != "":
order_number = create_id()
print "ORDER NUMBER", order_number
# print "rp ", request.POST
account_bank_name = request.POST['account_bank_name']
print "account_bank_name ", account_bank_name
amount_paid = request.POST['amount_paid']
print "amount_paid " , amount_paid
deposit_slip_number = request.POST['deposit_slip_number']
print "deposit_slip_number ", deposit_slip_number
payable = request.POST['payable']
print "payable ", payable
item, created = Order.objects.get_or_create(order_number=order_number, client=client, location=location, account_bank_name=account_bank_name, amount_paid=amount_paid, deposit_slip_number=deposit_slip_number, payable=payable)
item.save()
order = Order.objects.filter(order_number = order_number)
print "ORDER", order
all_items.update(ordered = True, order = order[0])
my_order = Order.objects.filter(client=request_user).filter(order_number=order_number) #Populate client's Order to template
tied_order = Cart.objects.filter(client=request_user, ordered=True, order=order)
print "TIED ORDER", tied_order
return render(request, "selly/summary.html", {'my_order': my_order, 'order': order, 'tied_order': tied_order, 'order_number': order_number})
每次重新加载浏览器时,都会创建一个新的订单号。
即使重新加载浏览器,如何修改订单号
答案 0 :(得分:0)
成功发布POST后总是重定向,即使它回到同一页面。
item, created = Order.objects.get_or_create(order_number=order_number, client=client, location=location, account_bank_name=account_bank_name, amount_paid=amount_paid, deposit_slip_number=deposit_slip_number, payable=payable)
item.save()
return redirect('summary')
答案 1 :(得分:0)
不要这样做,这会给简单的问题增加太多的复杂性。使用pre_save信号将订单保存到数据库时创建订单号。 Django 1.9 signals
因此,将带有create_id()代码的pre_save信号添加到Order对象。保存订单时会触发pre_save。