Django 實(shí)現(xiàn)購(gòu)物車(chē)功能的示例代碼
購(gòu)物車(chē)思路:使用 session 功能識(shí)別不同瀏覽器用戶(hù),使得用戶(hù)不管是否登錄了網(wǎng)站,均能夠把想要購(gòu)買(mǎi)的產(chǎn)品放在某個(gè)地方,之后隨時(shí)可以顯示或修改要購(gòu)買(mǎi)的產(chǎn)品,等確定了之后再下訂單,購(gòu)物車(chē)可以用來(lái)暫存商品。
我們可以使用 session 為每一個(gè)用戶(hù)創(chuàng)建一個(gè) ID,然后以這個(gè) ID 作為創(chuàng)建每一個(gè)購(gòu)物車(chē)的依據(jù)。這個(gè)購(gòu)物車(chē)在用戶(hù)瀏覽過(guò)程中會(huì)保留數(shù)據(jù),一直到實(shí)際完成下單,用戶(hù)執(zhí)行清除,或者關(guān)閉瀏覽器為止,當(dāng)然,退出登錄的話(huà)購(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 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 會(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_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)
購(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)品名稱(chēng)</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 %}
顯示如下:

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

