Django 實現(xiàn)購物車功能的示例代碼
購物車思路:使用 session 功能識別不同瀏覽器用戶,使得用戶不管是否登錄了網(wǎng)站,均能夠把想要購買的產(chǎn)品放在某個地方,之后隨時可以顯示或修改要購買的產(chǎn)品,等確定了之后再下訂單,購物車可以用來暫存商品。
我們可以使用 session 為每一個用戶創(chuàng)建一個 ID,然后以這個 ID 作為創(chuàng)建每一個購物車的依據(jù)。這個購物車在用戶瀏覽過程中會保留數(shù)據(jù),一直到實際完成下單,用戶執(zhí)行清除,或者關(guān)閉瀏覽器為止,當(dāng)然,退出登錄的話購物車內(nèi)容也會消失不見。
在 settings.py 文件中加入下列語句,表示要求在瀏覽器一關(guān)閉的時候 session 就會失效。
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
購物車的具體實現(xiàn)已經(jīng)有現(xiàn)成的模塊 django-cart 可以使用,詳細用法可以參考 GitHub:https://github.com/bmentges/django-cart 。執(zhí)行安裝。
pip install django-cart
安裝完成后我們在 settings.py 文件中 INSTALL_APPS 中加入 'cart' 模塊。并執(zhí)行 ./manage.py migrate 更新數(shù)據(jù)庫。
在 urls.py 中增加3個網(wǎng)站樣式,分別用來執(zhí)行購物車的增加產(chǎn)品,刪除產(chǎn)品以及查看購物車。
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 函數(shù),調(diào)用 django-cart 模塊的 Cart 類,實現(xiàn)增加產(chǎn)品功能。
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 會找不到這個模塊,報錯。
刪除產(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/')
顯示購物車內(nèi)容。
@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>產(chǎn)品名稱</td>
<td width=100 align=center>單價</td>
<td width=100 align=center>數(shù)量</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 %}
顯示如下:

至此,我們便完成了購物車功能,接下來可以實現(xiàn)訂單功能,付款功能等等。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解Python內(nèi)置函數(shù)map filter reduce及與列表推導(dǎo)式對比
這篇文章主要為大家介紹了Python內(nèi)置函數(shù)map filter reduce及與列表推導(dǎo)式對比方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)
這篇文章主要介紹了python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression),幫助大家更好的進行機器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10
Python實用庫 PrettyTable 學(xué)習(xí)筆記
這篇文章主要介紹了Python實用庫 PrettyTable 學(xué)習(xí)筆記,結(jié)合實例形式分析了Python表格操作庫PrettyTable的安裝、使用技巧與相關(guān)注意事項,需要的朋友可以參考下2019-08-08

