購(gòu)物車(chē)思路:使用 session 功能識(shí)別不同瀏覽器用戶,使得用戶不管是否登錄了網(wǎng)站,均能夠把想要購(gòu)買(mǎi)的產(chǎn)品放在某個(gè)地方,之后隨時(shí)可以顯示或修改要購(gòu)買(mǎi)的產(chǎn)品,等確定了之后再下訂單,購(gòu)物車(chē)可以用來(lái)暫存商品。
我們可以使用 session 為每一個(gè)用戶創(chuàng)建一個(gè) ID,然后以這個(gè) ID 作為創(chuàng)建每一個(gè)購(gòu)物車(chē)的依據(jù)。這個(gè)購(gòu)物車(chē)在用戶瀏覽過(guò)程中會(huì)保留數(shù)據(jù),一直到實(shí)際完成下單,用戶執(zhí)行清除,或者關(guān)閉瀏覽器為止,當(dāng)然,退出登錄的話購(gòu)物車(chē)內(nèi)容也會(huì)消失不見(jiàn)。
在 settings.py 文件中加入下列語(yǔ)句,表示要求在瀏覽器一關(guān)閉的時(shí)候 session 就會(huì)失效。
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
購(gòu)物車(chē)的具體實(shí)現(xiàn)已經(jīng)有現(xiàn)成的模塊 django-cart 可以使用,詳細(xì)用法可以參考 GitHub:https://github.com/bmentges/django-cart 。執(zhí)行安裝。
pip install django-cart
安裝完成后我們?cè)?settings.py 文件中 INSTALL_APPS 中加入 'cart' 模塊。并執(zhí)行 ./manage.py migrate 更新數(shù)據(jù)庫(kù)。
在 urls.py 中增加3個(gè)網(wǎng)站樣式,分別用來(lái)執(zhí)行購(gòu)物車(chē)的增加產(chǎn)品,刪除產(chǎn)品以及查看購(gòu)物車(chē)。
url(r'^cart/$', views.cart),url(r'^additem/(/d+)/(/d+)/$', views.add_to_cart, name='additem-url'),url(r'^removeitem/(/d+)/$', views.remove_from_cart, name='removeitem-url'),
我們編寫(xiě) add_to_cart 函數(shù),調(diào)用 django-cart 模塊的 Cart 類(lèi),實(shí)現(xiàn)增加產(chǎn)品功能。
from cart.cart import Cartdef add_to_cart(request, product_id, quantity): product = models.Product.objects.get(id=product_id) cart = Cart(request) cart.add(product, product.price, quantity) return redirect('/')
這里記得將 cart.py 中的 import models 改為 from . import models ,否則 Python 會(huì)找不到這個(gè)模塊,報(bào)錯(cuò)。
刪除產(chǎn)品。
def remove_from_cart(request, product_id): product = models.Product.objects.get(id=product_id) cart = Cart(request) cart.remove(product) return redirect('/cart/')
顯示購(gòu)物車(chē)內(nèi)容。
@login_requireddef cart(request): all_categories = models.Category.objects.all() cart = Cart(request) template = get_template('cart.html') html = template.render(context=locals(), request=request) return HttpResponse(html)
購(gòu)物車(chē)的 html 文件 cart.html 。
<!-- cart.html (mshop project) -->{% extends "base.html" %}{% block title %}查看購(gòu)物車(chē){% endblock %}{% block content %}<div class='container'>{% for message in messages %} <div class='alert alert-{{message.tags}}'>{{ message }}</div>{% endfor %} <div class='row'> <div class='col-md-12'> <div class='panel panel-default'> <div class='panel-heading' align=center> <h3>歡迎光臨迷你小電商</h3> {% if user.socialaccount_set.all.0.extra_data.name %} {{user.socialaccount_set.all.0.extra_data.name}}<br/> <img src='{{user.socialaccount_set.all.0.get_avatar_url}}' width='100'> {% else %} Welcome: {{ user.username }} {% endif %} </div> </div> </div> </div> <div class='row'> <div class='col-sm-12'> <div class='panel panel-info'> <div class='panel panel-heading'> <h4>我的購(gòu)物車(chē)</h4> </div> <div class='panel panel-body'> {% for item in cart %} {% if forloop.first %} <table border=1> <tr> <td width=300 align=center>產(chǎn)品名稱</td> <td width=100 align=center>單價(jià)</td> <td width=100 align=center>數(shù)量</td> <td width=100 align=center>小計(jì)</td> <td width=100 align=center>刪除</td> </tr> {% endif %} <div class='listgroup'> <div class='listgroup-item'> <tr> <td>{{ item.product.name }}</td> <td align=right>{{ item.product.price }}</td> <td align=center>{{ item.quantity }}</td> <td align=right>{{ item.total_price }}</td> <td align=center> <a href='{% url "removeitem-url" item.product.id %}'><span class='glyphicon glyphicon-trash'></span></a> </td> </tr> </div> </div> {% if forloop.last %} </table> <button class='btn btn-warning'><a href='/order'>我要訂購(gòu)</a></button> {% endif %} {% empty %} <em>購(gòu)物車(chē)是空的</em> {% endfor %} </div> <div class='panel panel-footer'> 總計(jì):{{ cart.summary }}元 </div> </div> </div> </div></div>{% endblock %}
新聞熱點(diǎn)
疑難解答
圖片精選