更新
This commit is contained in:
0
Python-100-Days/Day41-55/shop/cart/__init__.py
Normal file
0
Python-100-Days/Day41-55/shop/cart/__init__.py
Normal file
12
Python-100-Days/Day41-55/shop/cart/admin.py
Normal file
12
Python-100-Days/Day41-55/shop/cart/admin.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from cart.models import Goods
|
||||
|
||||
|
||||
class GoodsAdmin(admin.ModelAdmin):
|
||||
|
||||
list_display = ('id', 'name', 'price', 'image')
|
||||
search_fields = ('name', )
|
||||
|
||||
|
||||
admin.site.register(Goods, GoodsAdmin)
|
||||
5
Python-100-Days/Day41-55/shop/cart/apps.py
Normal file
5
Python-100-Days/Day41-55/shop/cart/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CartConfig(AppConfig):
|
||||
name = 'cart'
|
||||
@@ -0,0 +1,27 @@
|
||||
# Generated by Django 2.0.5 on 2018-05-25 06:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Goods',
|
||||
fields=[
|
||||
('id', models.AutoField(db_column='gid', primary_key=True, serialize=False)),
|
||||
('name', models.CharField(db_column='gname', max_length=50)),
|
||||
('price', models.DecimalField(db_column='gprice', decimal_places=2, max_digits=10)),
|
||||
('image', models.CharField(db_column='gimage', max_length=255)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'tb_goods',
|
||||
'ordering': ('id',),
|
||||
},
|
||||
),
|
||||
]
|
||||
15
Python-100-Days/Day41-55/shop/cart/models.py
Normal file
15
Python-100-Days/Day41-55/shop/cart/models.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Goods(models.Model):
|
||||
"""商品模型类"""
|
||||
|
||||
id = models.AutoField(primary_key=True, db_column='gid')
|
||||
name = models.CharField(max_length=50, db_column='gname')
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2, db_column='gprice')
|
||||
image = models.CharField(max_length=255, db_column='gimage')
|
||||
|
||||
class Meta:
|
||||
|
||||
db_table = 'tb_goods'
|
||||
ordering = ('id', )
|
||||
3
Python-100-Days/Day41-55/shop/cart/tests.py
Normal file
3
Python-100-Days/Day41-55/shop/cart/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
75
Python-100-Days/Day41-55/shop/cart/views.py
Normal file
75
Python-100-Days/Day41-55/shop/cart/views.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
from cart.models import Goods
|
||||
|
||||
|
||||
def index(request):
|
||||
goods_list = list(Goods.objects.all())
|
||||
return render(request, 'goods.html', {'goods_list': goods_list})
|
||||
|
||||
|
||||
class CartItem(object):
|
||||
"""购物车中的商品项"""
|
||||
|
||||
def __init__(self, goods, amount=1):
|
||||
self.goods = goods
|
||||
self.amount = amount
|
||||
|
||||
@property
|
||||
def total(self):
|
||||
return self.goods.price * self.amount
|
||||
|
||||
|
||||
class ShoppingCart(object):
|
||||
"""购物车"""
|
||||
|
||||
def __init__(self):
|
||||
self.items = {}
|
||||
|
||||
def add_item(self, item):
|
||||
if item.goods.id in self.items:
|
||||
self.items[item.goods.id].amount += item.amount
|
||||
else:
|
||||
self.items[item.goods.id] = item
|
||||
|
||||
def remove_item(self, id):
|
||||
if id in self.items:
|
||||
self.items.remove(id)
|
||||
|
||||
def clear_all_items(self):
|
||||
self.items.clear()
|
||||
|
||||
@property
|
||||
def cart_items(self):
|
||||
return self.items.values()
|
||||
|
||||
@property
|
||||
def total(self):
|
||||
val = 0
|
||||
for item in self.items.values():
|
||||
val += item.total
|
||||
return val
|
||||
|
||||
|
||||
def add_to_cart(request, id):
|
||||
goods = Goods.objects.get(pk=id)
|
||||
# 通过request对象的session属性可以获取到session
|
||||
# session相当于是服务器端用来保存用户数据的一个字典
|
||||
# session利用了Cookie保存sessionid
|
||||
# 通过sessionid就可以获取与某个用户对应的会话(也就是用户数据)
|
||||
# 如果在浏览器中清除了Cookie那么也就清除了sessionid
|
||||
# 再次访问服务器时服务器会重新分配新的sessionid这也就意味着之前的用户数据无法找回
|
||||
# 默认情况下Django的session被设定为持久会话而非浏览器续存期会话
|
||||
# 通过SESSION_EXPIRE_AT_BROWSER_CLOSE和SESSION_COOKIE_AGE参数可以修改默认设定
|
||||
# Django中的session是进行了持久化处理的因此需要设定session的序列化方式
|
||||
# 1.6版开始Django默认的session序列化器是JsonSerializer
|
||||
# 可以通过SESSION_SERIALIZER来设定其他的序列化器(例如PickleSerializer)
|
||||
cart = request.session.get('cart', ShoppingCart())
|
||||
cart.add_item(CartItem(goods))
|
||||
request.session['cart'] = cart
|
||||
return redirect('/')
|
||||
|
||||
|
||||
def show_cart(request):
|
||||
cart = request.session.get('cart', None)
|
||||
return render(request, 'cart.html', {'cart': cart})
|
||||
Reference in New Issue
Block a user