訂單管理
=我的訂單========
base.html
<a href="{% url 'vip_orders' %}" class="layout-user-downmenu-link" data-mtype="wmz_public_grzx_myorder">我的订单</a>
urls.py
url(r'^vip/orders$', vip.viporders,name='vip_orders'), #会员中心我的订单
vip.py
def viporders(request):
context = loadinfo(request)
odlist = Orders.objects.filter(uid=request.session['vipuser']['id'])
# 用登錄者ID去查詢訂單Orders() 調取該人的所有訂單訊息(有幾張訂單,訂單號碼多少..etc)
for od in odlist:
# 將每筆訂單訊息逐一取出
dlist = Detail.objects.filter(orderid=od.id)
# 用訂單號od.id去查詢訂單商品詳情
for og in dlist:
# 將每筆訂單商品詳情逐一取出
og.picname = Goods.objects.only('picname').get(id=og.goodsid).picname
# 並賦予新屬性picname, 用Detail().goodsid去查詢商品表單Goods().id, 並只取picname為值
od.detaillist = dlist
# 賦予新屬性,並指向dlist
context['orderslist'] = odlist
context['dlist'] = dlist
return render(request,"web/viporders.html",context)
*QuerySet is iterable and assignable
while Object is only assignable
to add new data into dict, either use dict.update({new key:new value}) or
dict[0] = {newkey or new dict{newkey:newvalue}}
viporders.html
<!-- 每个订单信息 -->
{% for orders in orderslist %}
订单号:<span class="time">{{ orders.id }}</span>
收货人:<span class="orderNumber">{{ orders.linkman }}</span>
收货地址:<span class="orderNumber">{{ orders.address }}</span>
联系电话:<span class="orderNumber">{{ orders.phone }}</span>
{% for detail in orders.detaillist %}
<!-- 點擊圖片時,回到detail同時傳送商品id-->
<a class="productDetail nameWidth col-xs-4 col-sm-4" href="{% url 'detail' detail.goodsid %}" target="_blank">
<img src="/static/goods/s_{{detail.picname}}" style="width:75px;height:75px" class="f-fl"/>
</a>
{{ detail.name }}
¥{{ detail.price}}×{{ detail.num}}
{% endfor %}
¥ {{ orders.total }} 元
<!-- 判斷訂單的state狀態,顯示對應的文字及超連結-->
{% if orders.state == 0 %}
新订单 <br/><br/><br/>
【<a href="{% url 'vip_odstate' %}?oid={{orders.id}}&state=3">撤销订单</a>】
{% elif orders.state == 1 %}
已发货 <br/><br/>
【<a href="{% url 'vip_odstate' %}?oid={{orders.id}}&state=2">确认收货</a>】
{% elif orders.state == 2 %}
已完成
{% elif orders.state == 3 %}
无效订单
{% else %}
未知
{% endif %}
{% endfor %}
urls.py
url(r'^vip/odstate$', vip.odstate,name='vip_odstate'), #修改订单状态(确认收货)
vip.py
def odstate(request):
''' 修改订单状态 '''
try:
oid = request.GET.get("oid",'0')
ob = Orders.objects.get(id=oid)
ob.state = request.GET['state']
ob.save()
return redirect(reverse('vip_orders'))
except Exception as err:
print(err)
return HttpResponse("订单处理失败!")