In my project, I have an order detail
page. This page has multiple forms (return order, deliver order etc.)
在我的项目中,我有一个订单细节页面。本页面有多种形式(退货、送货等)。
Each of this forms are handled using different views.
每个表单都使用不同的视图进行处理。
For example returning order form has action which calls order_return
view which then, if ReturnOrderForm
is valid, redirects back to order_detail
.
例如,返回order表单具有调用order_return视图的操作,如果ReturnOrderForm是有效的,那么该操作将重定向回order_detail。
The problem is when there are errors in the ReturnOrderForm
. I would like to get back to order_detail
and show the errors.
问题是当ReturnOrderForm出现错误时。我想回到order_detail并显示错误。
For now, I just render order_detail
inside the order_return
view in case there are errors, which works good but the url isn't changed to order_detail
url. I would use HttpResponseRedirect
but I have no idea how to handle errors.
现在,我只是在order_return视图中呈现order_detail,以防出现错误,这很有效,但是url没有被更改为order_detail url。我会使用HttpResponseRedirect,但我不知道如何处理错误。
Do you have any ideas?
你有什么想法吗?
def order_return(request):
return_order_form = ReturnOrderForm(request.POST or None)
order_id = request.POST.get('order_id')
order = Job.objects.get(id=order_id)
if request.method == 'POST':
if return_order_form.is_valid():
...
order.delivery.return_delivery(notes=customer_notes)
return HttpResponseRedirect(reverse("order_detail", args=(order_id,)))
return render(request, "ordersapp/order_detail/order-detail.html",
context={'return_order_form': return_order_form,
...})
1 个解决方案
#1
1
You can make a shared method that can return the detail response
您可以创建一个可以返回细节响应的共享方法
def get_order_detail_response(request, contxt=None, **kwargs)
context = contxt or {}
context['return_order_form'] = kwargs.get('return_form', ReturnOrderForm())
return render(request, 'template', context)
Each view will then be able to return this
每个视图将能够返回这个
return get_order_detail_response(request, return_form=return_order_form)
#1
1
You can make a shared method that can return the detail response
您可以创建一个可以返回细节响应的共享方法
def get_order_detail_response(request, contxt=None, **kwargs)
context = contxt or {}
context['return_order_form'] = kwargs.get('return_form', ReturnOrderForm())
return render(request, 'template', context)
Each view will then be able to return this
每个视图将能够返回这个
return get_order_detail_response(request, return_form=return_order_form)