亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Django 實現(xiàn)購物車功能的示例代碼

 更新時間:2018年10月08日 09:45:47   作者:希希里之海  
這篇文章主要介紹了Django 實現(xiàn)購物車功能的示例代碼,實現(xiàn)了刪除產(chǎn)品和顯示購物車的一系列購物車的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

購物車思路:使用 session 功能識別不同瀏覽器用戶,使得用戶不管是否登錄了網(wǎng)站,均能夠把想要購買的產(chǎn)品放在某個地方,之后隨時可以顯示或修改要購買的產(chǎn)品,等確定了之后再下訂單,購物車可以用來暫存商品。

我們可以使用 session 為每一個用戶創(chuàng)建一個 ID,然后以這個 ID 作為創(chuàng)建每一個購物車的依據(jù)。這個購物車在用戶瀏覽過程中會保留數(shù)據(jù),一直到實際完成下單,用戶執(zhí)行清除,或者關閉瀏覽器為止,當然,退出登錄的話購物車內(nèi)容也會消失不見。

在 settings.py 文件中加入下列語句,表示要求在瀏覽器一關閉的時候 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ù),調用 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python數(shù)據(jù)處理之如何修改索引和行列

    python數(shù)據(jù)處理之如何修改索引和行列

    這篇文章主要介紹了python數(shù)據(jù)處理之如何修改索引和行列問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 深入理解Python內(nèi)置函數(shù)map filter reduce及與列表推導式對比

    深入理解Python內(nèi)置函數(shù)map filter reduce及與列表推導式對比

    這篇文章主要為大家介紹了Python內(nèi)置函數(shù)map filter reduce及與列表推導式對比方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)

    python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)

    這篇文章主要介紹了python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression),幫助大家更好的進行機器學習,感興趣的朋友可以了解下
    2020-10-10
  • Python基礎之字典的詳細使用教程

    Python基礎之字典的詳細使用教程

    字典作為Python的一個內(nèi)置數(shù)據(jù)結構,和列表一樣都是可變序列的,但是它是無序的,以鍵值對的方式存儲數(shù)據(jù)。本文將詳解一下Python中字典的使用,需要的可以參考一下
    2022-07-07
  • python字符串常用方法

    python字符串常用方法

    這篇文章主要介紹了python字符串常用方法,find、count、replace、split、startswith、endswith等多種方法,需要的朋友可以參考一下文章得具體內(nèi)容,希望對你有所幫助
    2021-10-10
  • python之語音識別speech模塊

    python之語音識別speech模塊

    這篇文章主要介紹了python之語音識別speech模塊,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Python實現(xiàn)程序的單一實例用法分析

    Python實現(xiàn)程序的單一實例用法分析

    這篇文章主要介紹了Python實現(xiàn)程序的單一實例用法,較為詳細的分析了Python窗口的相關操作技巧,需要的朋友可以參考下
    2015-06-06
  • 詳解如何理解并正確使用Python中的f字符串

    詳解如何理解并正確使用Python中的f字符串

    Python中的f字符串是一種字符串格式化語法,它可以將變量、表達式和函數(shù)等動態(tài)地嵌入到字符串中,本文就來詳細講講如何理解并正確使用它吧
    2023-06-06
  • Python實用庫 PrettyTable 學習筆記

    Python實用庫 PrettyTable 學習筆記

    這篇文章主要介紹了Python實用庫 PrettyTable 學習筆記,結合實例形式分析了Python表格操作庫PrettyTable的安裝、使用技巧與相關注意事項,需要的朋友可以參考下
    2019-08-08
  • 一篇文章帶你了解幾個好用的Python技巧

    一篇文章帶你了解幾個好用的Python技巧

    這篇文章主要介紹了幾個Python小技巧,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10

最新評論