django-鎻愪氦璁㈠崟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django-鎻愪氦璁㈠崟相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ram' title='ram'>ram
ret stat url join 鑾峰彇鍙傛暟 render 杩斿洖 浼犲弬璐墿杞art.html椤甸潰鍔爁orm琛ㄥ崟鎻愪氦
<form method="post" action="{% url 鈥榦rder:place鈥?%}"> {# 鎻愪氦鍒?order/place椤甸潰澶勭悊 #} {% for sku in skus %} <ul class="cart_list_td clearfix"> <li class="col01"><input type="checkbox" name="sku_ids" value="{{ sku.id }}" checked></li> <li class="col02"><img src="{{ sku.image.url }}"></li> <li class="col03">{{ sku.name }}<br><em>{{ sku.price }}鍏?{{ sku.unite }}</em></li> <li class="col04">{{ sku.unite }}</li> <li class="col05">{{ sku.price }}鍏?span style="color: #0000ff;"></li> <li class="col06"> <div class="num_add"> <a href="javascript:;" class="add fl">+</a> <input type="text" sku_id="{{ sku.id }}" class="num_show fl" value="{{ sku.count }}"> <a href="javascript:;" class="minus fl">-</a> </div> </li> <li class="col07">{{ sku.amount }}鍏?span style="color: #0000ff;"></li> <li class="col08"><a href="javascript:;">鍒犻櫎</a></li> </ul> {% endfor %} <ul class="settlements"> {% csrf_token %} <li class="col01"><input type="checkbox" name="" checked=""></li> <li class="col02">鍏ㄩ€?span style="color: #0000ff;"></li> <li class="col03">鍚堣(涓嶅惈杩愯垂)锛?span style="color: #0000ff;"><span>¥</span><em>{{ total_price }}</em><br>鍏辫<b>{{ total_count }}</b>浠跺晢鍝?span style="color: #0000ff;"></li> <li class="col04"><input type="submit" value="鍘荤粨绠?></li> </ul> </form>
瑙嗗浘鍑芥暟views.py娣诲姞orderplace鍔熻兘
from django.shortcuts import render, redirect from django.core.urlresolvers import reverse from django.http import JsonResponse from django.views.generic import View from user.models import Address from goods.models import GoodsSKU from orders.models import OrderInfo, OrderGoods from django_redis import get_redis_connection # 杩炴帴redis from utils.mixin import LoginRequiredMinxin # 鐧诲綍楠岃瘉 from datetime import datetime # Create your views here. # /order/place class OrderPlaceView(LoginRequiredMinxin, View): 鈥樷€樷€?/span>鎻愪氦璁㈠崟椤甸潰鏄剧ず鈥樷€樷€?/span> def post(self, request): 鈥樷€樷€?/span>鎻愪氦璁㈠崟椤甸潰鏄剧ず鈥樷€樷€?/span> # 鑾峰彇鐧诲綍鐨勭敤鎴?/span> user = request.user # 鑾峰彇鍙傛暟sku_ids sku_ids = request.POST.getlist(鈥?/span>sku_ids鈥?/span>) # [1,26] # 鏍¢獙鍙傛暟 if not sku_ids: # 璺宠浆鍒拌喘鐗╄溅椤甸潰 return redirect(reverse(鈥?/span>cart:show鈥?/span>)) conn = get_redis_connection(鈥?/span>default鈥?/span>) cart_key = 鈥?/span>cart_%d鈥?/span>%user.id skus = [] # 淇濆瓨鍟嗗搧鐨勬€讳欢鏁板拰鎬讳环鏍?/span> total_count = 0 total_price = 0 # 閬嶅巻sku_ids鑾峰彇鐢ㄦ埛瑕佽喘涔扮殑鍟嗗搧鐨勪俊鎭?/span> for sku_id in sku_ids: # 鏍规嵁鍟嗗搧鐨刬d鑾峰彇鍟嗗搧鐨勪俊鎭?/span> sku = GoodsSKU.objects.get(id=sku_id) # 鑾峰彇鐢ㄦ埛鎵€瑕佽喘涔扮殑鍟嗗搧鐨勬暟閲?/span> count = conn.hget(cart_key, sku_id) # 璁$畻鍟嗗搧鐨勫皬璁?/span> amount = sku.price*int(count) # 鍔ㄦ€佺粰sku澧炲姞灞炴€ount,淇濆瓨璐拱鍟嗗搧鐨勬暟閲?/span> sku.count = count # 鍔ㄦ€佺粰sku澧炲姞灞炴€mount,淇濆瓨璐拱鍟嗗搧鐨勫皬璁?/span> sku.amount = amount # 杩藉姞 skus.append(sku) # 绱姞璁$畻鍟嗗搧鐨勬€讳欢鏁板拰鎬讳环鏍?/span> total_count += int(count) total_price += amount # 杩愯垂:瀹為檯寮€鍙戠殑鏃跺€欙紝灞炰簬涓€涓瓙绯荤粺 transit_price = 10 # 鍐欐 # 瀹炰粯娆?/span> total_pay = total_price + transit_price # 鑾峰彇鐢ㄦ埛鐨勬敹浠跺湴鍧€ addrs = Address.objects.filter(user=user) # 缁勭粐涓婁笅鏂?/span> sku_ids = 鈥?/span>,鈥?/span>.join(sku_ids) # [1,25]->1,25 鎶婂垪琛ㄦ嫾鎺ユ垚瀛楃涓插舰寮忎紶鍙?/span> context = {鈥?/span>skus鈥?/span>:skus, 鈥?/span>total_count鈥?/span>:total_count, 鈥?/span>total_price鈥?/span>:total_price, 鈥?/span>transit_price鈥?/span>:transit_price, 鈥?/span>total_pay鈥?/span>:total_pay, 鈥?/span>addrs鈥?/span>:addrs, 鈥?/span>sku_ids鈥?/span>:sku_ids} # 浣跨敤妯℃澘 return render(request, 鈥?/span>place_order.html鈥?/span>, context)
妯℃澘order_place.html
{% extends 鈥榣ayout/base_no_cart.html鈥?%} {% load staticfiles %} {% block title %}澶╁ぉ鐢熼矞-鎻愪氦璁㈠崟{% endblock title %} {% block page_title %}鎻愪氦璁㈠崟{% endblock page_title %} {% block body %} <h3 class="common_title">纭鏀惰揣鍦板潃</h3> <div class="common_list_con clearfix"> <dl> <dt>瀵勯€佸埌锛?span style="color: #0000ff;"></dt> {% for addr in addrs %} <dd><input type="radio" name="addr_id" value="{{ addr.id }}" {% if addr.is_default %}checked{% endif %}>{{ addr.addr }} 锛坽{ addr.receiver }} 鏀讹級 {{ addr.phone }}</dd> {% endfor %} </dl> <a href="{% url 鈥榰ser:address鈥?%}" class="edit_site">缂栬緫鏀惰揣鍦板潃</a> </div> <h3 class="common_title">鏀粯鏂瑰紡</h3> <div class="common_list_con clearfix"> <div class="pay_style_con clearfix"> <input type="radio" name="pay_style" value="1" checked> <label class="cash">璐у埌浠樻</label> <input type="radio" name="pay_style" value="2"> <label class="weixin">寰俊鏀粯</label> <input type="radio" name="pay_style" value="3"> <label class="zhifubao"></label> <input type="radio" name="pay_style" value="4"> <label class="bank">閾惰鍗℃敮浠?span style="color: #0000ff;"></label> </div> </div> <h3 class="common_title">鍟嗗搧鍒楄〃</h3> <div class="common_list_con clearfix"> <ul class="goods_list_th clearfix"> <li class="col01">鍟嗗搧鍚嶇О</li> <li class="col02">鍟嗗搧鍗曚綅</li> <li class="col03">鍟嗗搧浠锋牸</li> <li class="col04">鏁伴噺</li> <li class="col05">灏忚</li> </ul> {% for sku in skus %} <ul class="goods_list_td clearfix"> <li class="col01">{{ forloop.counter }}</li> <li class="col02"><img src="{{ sku.image.url }}"></li> <li class="col03">{{ sku.name }}</li> <li class="col04">{{ sku.unite }}</li> <li class="col05">{{ sku.price }}鍏?span style="color: #0000ff;"></li> <li class="col06">{{ sku.count }}</li> <li class="col07">{{ sku.amount }}鍏?span style="color: #0000ff;"></li> </ul> {% endfor %} </div> <h3 class="common_title">鎬婚噾棰濈粨绠?span style="color: #0000ff;"></h3> <div class="common_list_con clearfix"> <div class="settle_con"> <div class="total_goods_count">鍏?span style="color: #0000ff;"><em>{{ total_count }}</em>浠跺晢鍝侊紝鎬婚噾棰?span style="color: #0000ff;"><b>{{ total_price }}鍏?span style="color: #0000ff;"></b></div> <div class="transit">杩愯垂锛?span style="color: #0000ff;"><b>{{ transit_price }}鍏?span style="color: #0000ff;"></b></div> <div class="total_pay">瀹炰粯娆撅細<b>{{ total_pay }}鍏?span style="color: #0000ff;"></b></div> </div> </div> <div class="order_submit clearfix"> {% csrf_token %} <a href="javascript:;" sku_ids={{ sku_ids }} id="order_btn">鎻愪氦璁㈠崟</a> </div> {% endblock body %} {% block bottom %} <div class="popup_con"> <div class="popup"> <p>璁㈠崟鎻愪氦鎴愬姛锛?span style="color: #0000ff;"></p> </div> <div class="mask"></div> </div> {% endblock bottom %} {% block bottomfiles %} <script type="text/javascript" src="{% static 鈥榡s/jquery-1.12.4.min.js鈥?%}"></script> <script type="text/javascript"> $(鈥?/span>#order_btn鈥?/span>).click(function() { // 鑾峰彇鐢ㄦ埛閫夋嫨鐨勫湴鍧€id, 鏀粯鏂瑰紡, 瑕佽喘涔扮殑鍟嗗搧id瀛楃涓?/span> addr_id = $(鈥?/span>input[name="addr_id"]:checked鈥?/span>).val() // 鑾峰彇鍦板潃 pay_method = $(鈥?/span>input[name="pay_style"]:checked鈥?/span>).val() // 鑾峰彇鏀粯鏂瑰紡 sku_ids = $(this).attr(鈥?/span>sku_ids鈥?/span>) // 鑾峰彇鍟嗗搧id鎷兼帴鐨勫瓧绗︿覆 csrf = $(鈥?/span>input[name="csrfmiddlewaretoken"]鈥?/span>).val() // alert(addr_id+":"+pay_method+鈥?鈥?sku_ids) // 缁勭粐鍙傛暟 params = {鈥?/span>addr_id鈥?/span>:addr_id, 鈥?/span>pay_method鈥?/span>:pay_method, 鈥?/span>sku_ids鈥?/span>:sku_ids, 鈥?/span>csrfmiddlewaretoken鈥?/span>:csrf} // 鍙戣捣ajax post璇锋眰锛岃闂?order/commit, 浼犻€掔殑鍙傛暟: addr_id pay_method, sku_ids $.post(鈥?/span>/order/commit鈥?/span>, params, function (data) { if (data.res == 5){ // 鍒涘缓鎴愬姛 alert(鈥?/span>鍒涘缓鎴愬姛鈥?/span>) } else{ alert(data.errmsg) } }) {# localStorage.setItem(鈥?/span>order_finish鈥?/span>,2);#} {##} {# $(鈥?/span>.popup_con鈥?/span>).fadeIn(鈥?/span>fast鈥?/span>, function() {#} {##} {# setTimeout(function(){#} {# $(鈥?/span>.popup_con鈥?/span>).fadeOut(鈥?/span>fast鈥?/span>,function(){#} {# window.location.href = 鈥?/span>index.html鈥?/span>;#} {# }); #} {# },3000)#} {# #} {# });#} }); </script> {% endblock bottomfiles %}
鍚庡彴views.py澶勭悊璁㈠崟鍔熻兘orderCommit
# 鍓嶇浼犻€掔殑鍙傛暟:鍦板潃id(addr_id) 鏀粯鏂瑰紡(pay_method) 鐢ㄦ埛瑕佽喘涔扮殑鍟嗗搧id瀛楃涓?sku_ids) # mysql浜嬪姟: 涓€缁剆ql鎿嶄綔锛岃涔堥兘鎴愬姛锛岃涔堥兘澶辫触 # 楂樺苟鍙?绉掓潃 # 鏀粯瀹濇敮浠?/span> class OrderCommitView(View): 鈥樷€樷€?/span>璁㈠崟鍒涘缓鈥樷€樷€?/span> def post(self, request): 鈥樷€樷€?/span>璁㈠崟鍒涘缓鈥樷€樷€?/span> # 鍒ゆ柇鐢ㄦ埛鏄惁鐧诲綍 user = request.user if not user.is_authenticated(): # 鐢ㄦ埛鏈櫥褰?/span> return JsonResponse({鈥?/span>res鈥?/span>:0, 鈥?/span>errmsg鈥?/span>:鈥?/span>鐢ㄦ埛鏈櫥褰?/span>鈥?/span>}) # 鎺ユ敹鍙傛暟 addr_id = request.POST.get(鈥?/span>addr_id鈥?/span>) pay_method = request.POST.get(鈥?/span>pay_method鈥?/span>) sku_ids = request.POST.get(鈥?/span>sku_ids鈥?/span>) # 1,3 # 鏍¢獙鍙傛暟 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({鈥?/span>res鈥?/span>:1, 鈥?/span>errmsg鈥?/span>:鈥?/span>鍙傛暟涓嶅畬鏁?/span>鈥?/span>}) # 鏍¢獙鏀粯鏂瑰紡 if pay_method not in OrderInfo.PAY_METHODS.keys(): # 闇€瑕佸湪orders/model.py涓坊鍔燩AY_METHODS瀛楀吀 return JsonResponse({鈥?/span>res鈥?/span>:2, 鈥?/span>errmsg鈥?/span>:鈥?/span>闈炴硶鐨勬敮浠樻柟寮?/span>鈥?/span>}) # 鏍¢獙鍦板潃 try: addr = Address.objects.get(id=addr_id) except Address.DoesNotExist: # 鍦板潃涓嶅瓨鍦?/span> return JsonResponse({鈥?/span>res鈥?/span>:3, 鈥?/span>errmsg鈥?/span>:鈥?/span>鍦板潃闈炴硶鈥?/span>}) # todo: 鍒涘缓璁㈠崟鏍稿績涓氬姟 # 缁勭粐鍙傛暟 # 璁㈠崟id: 20171122181630+鐢ㄦ埛id order_id = datetime.now().strftime(鈥?/span>%Y%m%d%H%M%S鈥?/span>)+str(user.id) # 杩愯垂 transit_price = 10 # 鎬绘暟鐩拰鎬婚噾棰?/span> total_count = 0 total_price = 0 # todo: 鍚慸f_order_info琛ㄤ腑娣诲姞涓€鏉¤褰?/span> order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price) # todo: 鐢ㄦ埛鐨勮鍗曚腑鏈夊嚑涓晢鍝侊紝闇€瑕佸悜df_order_goods琛ㄤ腑鍔犲叆鍑犳潯璁板綍 conn = get_redis_connection(鈥?/span>default鈥?/span>) cart_key = 鈥?/span>cart_%d鈥?/span>%user.id sku_ids = sku_ids.split(鈥?/span>,鈥?/span>) for sku_id in sku_ids: # 鑾峰彇鍟嗗搧鐨勪俊鎭?/span> try: sku = GoodsSKU.objects.get(id=sku_id) except: # 鍟嗗搧涓嶅瓨鍦?/span> return JsonResponse({鈥?/span>res鈥?/span>:4, 鈥?/span>errmsg鈥?/span>:鈥?/span>鍟嗗搧涓嶅瓨鍦?/span>鈥?/span>}) # 浠巖edis涓幏鍙栫敤鎴锋墍瑕佽喘涔扮殑鍟嗗搧鐨勬暟閲?/span> count = conn.hget(cart_key, sku_id) # todo: 鍚慸f_order_goods琛ㄤ腑娣诲姞涓€鏉¤褰?/span> OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # todo: 鏇存柊鍟嗗搧鐨勫簱瀛樺拰閿€閲?/span> sku.stock -= int(count) sku.sales += int(count) sku.save() # todo: 绱姞璁$畻璁㈠崟鍟嗗搧鐨勬€绘暟閲忓拰鎬讳环鏍?/span> amount = sku.price*int(count) total_count += int(count) total_price += amount # todo: 鏇存柊璁㈠崟淇℃伅琛ㄤ腑鐨勫晢鍝佺殑鎬绘暟閲忓拰鎬讳环鏍?/span> order.total_count = total_count order.total_price = total_price order.save() # todo: 娓呴櫎鐢ㄦ埛璐墿杞︿腑瀵瑰簲鐨勮褰?/span> conn.hdel(cart_key, *sku_ids) # 杩斿洖搴旂瓟 return JsonResponse({鈥?/span>res鈥?/span>:5, 鈥?/span>message鈥?/span>:鈥?/span>鍒涘缓鎴愬姛鈥?/span>})
姝ゆ椂闇€瑕佸湪orders/models.py涓璒rderInfo鏂规硶涓坊鍔犲瓧鍏?/p>
class OrderInfo(BaseModel): 鈥樷€樷€?/span>璁㈠崟妯″瀷绫?/span>鈥樷€樷€?/span> PAY_METHODS = { 鈥?/span>1鈥?/span>: "璐у埌浠樻", 鈥?/span>2鈥?/span>: "寰俊鏀粯", 鈥?/span>3鈥?/span>: "鏀粯瀹?/span>", 鈥?/span>4鈥?/span>: 鈥?/span>閾惰仈鏀粯鈥?/span> } PAY_METHODS_ENUM = { "CASH": 1, "ALIPAY": 2 } ORDER_STATUS_ENUM = { "UNPAID": 1, "UNSEND": 2, "UNRECEIVED": 3, "UNCOMMENT": 4, "FINISHED": 5 }