購物車思路:使用 session 功能識別不同瀏覽器用戶,使得用戶不管是否登錄了網站,均能夠把想要購買的產品放在某個地方,之后隨時可以顯示或修改要購買的產品,等確定了之后再下訂單,購物車可以用來暫存商品。
我們可以使用 session 為每一個用戶創建一個 ID,然后以這個 ID 作為創建每一個購物車的依據。這個購物車在用戶瀏覽過程中會保留數據,一直到實際完成下單,用戶執行清除,或者關閉瀏覽器為止,當然,退出登錄的話購物車內容也會消失不見。
在 settings.py 文件中加入下列語句,表示要求在瀏覽器一關閉的時候 session 就會失效。
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
購物車的具體實現已經有現成的模塊 django-cart 可以使用,詳細用法可以參考 GitHub:https://github.com/bmentges/django-cart 。執行安裝。
pip install django-cart
安裝完成后我們在 settings.py 文件中 INSTALL_APPS 中加入 'cart' 模塊。並執行 ./manage.py migrate 更新數據庫。
在 urls.py 中增加3個網站樣式,分別用來執行購物車的增加產品,刪除產品以及查看購物車。
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'),
我們編寫 add_to_cart 函數,調用 django-cart 模塊的 Cart 類,實現增加產品功能。
from cart.cart import Cart def 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 會找不到這個模塊,報錯。
刪除產品。
def remove_from_cart(request, product_id): product = models.Product.objects.get(id=product_id) cart = Cart(request) cart.remove(product) return redirect('/cart/')
顯示購物車內容。
@login_required def 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)
購物車的 html 文件 cart.html 。
<!-- cart.html (mshop project) --> {% extends "base.html" %} {% block title %}查看購物車{% 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>我的購物車</h4>
</div>
<div class='panel panel-body'> {% for item in cart %} {% if forloop.first %} <table border=1>
<tr>
<td width=300 align=center>產品名稱</td>
<td width=100 align=center>單價</td>
<td width=100 align=center>數量</td>
<td width=100 align=center>小計</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'>我要訂購</a></button> {% endif %} {% empty %} <em>購物車是空的</em> {% endfor %} </div>
<div class='panel panel-footer'> 總計:{{ cart.summary }}元 </div>
</div>
</div>
</div>
</div> {% endblock %}
顯示如下:
至此,我們便完成了購物車功能,接下來可以實現訂單功能,付款功能等等。