提交 95f78d2d 编写于 作者: 骆昊的技术专栏's avatar 骆昊的技术专栏

更新了购物车案例的代码

上级 169188f8
......@@ -6,6 +6,7 @@ from cart.models import Goods
class GoodsAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'price', 'image')
search_fields = ('name', )
admin.site.register(Goods, GoodsAdmin)
# Generated by Django 2.0.5 on 2018-05-25 05:11
# Generated by Django 2.0.5 on 2018-05-25 06:28
from django.db import migrations, models
......
......@@ -2,6 +2,7 @@ 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')
......@@ -9,5 +10,6 @@ class Goods(models.Model):
image = models.CharField(max_length=255, db_column='gimage')
class Meta:
db_table = 'tb_goods'
ordering = ('id',)
ordering = ('id', )
from django.shortcuts import render
from django.shortcuts import render, redirect
from cart.models import Goods
......@@ -8,9 +8,68 @@ def index(request):
return render(request, 'goods.html', {'goods_list': goods_list})
def show_cart(request):
return render(request, 'cart.html')
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 add_to_cart(request, no):
pass
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})
文件模式从 100755 更改为 100644
import pymysql
pymysql.install_as_MySQLdb()
\ No newline at end of file
pymysql.install_as_MySQLdb()
......@@ -20,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+gqc54!5+uhvc^o0)fjvihmg&5uu^u+#s5m*fc+e+@bw*(+!w*'
SECRET_KEY = '3(n^av%_kt*^2zhz0!iwkxv6_wp^ed7-dpow*vqr7ck0_6=9^e'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
......@@ -37,7 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cart',
'cart.apps.CartConfig',
]
MIDDLEWARE = [
......@@ -78,7 +78,7 @@ WSGI_APPLICATION = 'shop.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Shop',
'NAME': 'shop',
'HOST': 'localhost',
'PORT': 3306,
'USER': 'root',
......@@ -105,11 +105,12 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Chongqing'
......@@ -122,5 +123,7 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_URL = '/static/'
......@@ -20,7 +20,7 @@ from cart import views
urlpatterns = [
path('', views.index),
path('add_to_cart/<int:id>', views.add_to_cart),
path('show_cart', views.show_cart),
path('add_to_cart/<int:no>', views.add_to_cart),
path('admin/', admin.site.urls),
]
......@@ -21,9 +21,9 @@
<hr>
</div>
<div class="right">
<a href="list_goods" class="back">返回</a>
<a href="/" class="back">返回</a>
</div>
{% if cart_items %}
{% if cart %}
<table style="clear: both;">
<tr>
<th>商品名称</th>
......@@ -32,12 +32,12 @@
<th>商品总价</th>
<th>操作</th>
</tr>
{% for item in cart_items %}
{% for item in cart.cart_items %}
<tr>
<td class="name">{{ item.name }}</td>
<td class="price">&yen;{{ item.unit_price }}</td>
<td class="name">{{ item.goods.name }}</td>
<td class="price">&yen;{{ item.goods.price }}</td>
<td>{{ item.amount }}</td>
<td class="price">&yen;{{ item.total_price }}</td>
<td class="price">&yen;{{ item.total }}</td>
<td>
<a href="" class="del">删除</a>
</td>
......@@ -47,7 +47,7 @@
<td colspan="5" class="total price">&yen;{{ cart.total }}元</td>
</tr>
</table>
<a href="clear_cart" class="back">清空购物车</a>
<a href="" class="back">清空购物车</a>
{% else %}
<h3 style="clear: both;">购物车中暂时没有商品!</h3>
{% endif %}
......
from django.contrib import admin
from cart.models import Goods
class GoodsAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'price', 'image')
admin.site.register(Goods, GoodsAdmin)
from django.apps import AppConfig
class CartConfig(AppConfig):
name = 'cart'
# Generated by Django 2.0.5 on 2018-05-25 05:11
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',),
},
),
]
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',)
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
from cart.models import Goods
def index(request):
goods_list = list(Goods.objects.all())
return render(request, 'goods.html', {'goods_list': goods_list})
def show_cart(request):
return render(request, 'cart.html')
def add_to_cart(request, no):
pass
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shop.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
import pymysql
pymysql.install_as_MySQLdb()
\ No newline at end of file
"""
Django settings for shop project.
Generated by 'django-admin startproject' using Django 2.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+gqc54!5+uhvc^o0)fjvihmg&5uu^u+#s5m*fc+e+@bw*(+!w*'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cart',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'shop.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'shop.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Shop',
'HOST': 'localhost',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '123456',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Chongqing'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
"""shop URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from cart import views
urlpatterns = [
path('', views.index),
path('show_cart', views.show_cart),
path('add_to_cart/<int:no>', views.add_to_cart),
path('admin/', admin.site.urls),
]
"""
WSGI config for shop project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shop.settings")
application = get_wsgi_application()
drop database if exists Shop;
create database Shop default charset utf8;
use Shop;
create table tb_goods
(
gid int not null auto_increment,
gname varchar(50) not null,
gprice decimal(10,2) not null,
gimage varchar(255),
primary key (gid)
);
insert into tb_goods values
(default, '乐事(Lay’s)无限薯片', 8.2, 'images/lay.jpg'),
(default, '旺旺 仙贝 加量装 540g', 18.5, 'images/wang.jpg'),
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
th, td { margin: 0; padding: 0; width: 180px; text-align: left; }
.name { font-size: 14px; font-weight: bolder; width: 280px; }
.price { color: red; font-size: 18px; }
a { display: inline-block; text-align: center; background-color: red; }
.back { width: 120px; height: 30px; line-height: 30px; }
.del { width: 60px; height: 20px; line-height: 20px; }
a:link, a:visited { color: white; text-decoration: none; }
.left { float: left; width: 1000px;}
.right { float: right; }
.total { text-align: right; }
</style>
</head>
<body>
<div class="left">
<h1>购物车列表</h1>
<hr>
</div>
<div class="right">
<a href="list_goods" class="back">返回</a>
</div>
{% if cart_items %}
<table style="clear: both;">
<tr>
<th>商品名称</th>
<th>商品单价</th>
<th>商品数量</th>
<th>商品总价</th>
<th>操作</th>
</tr>
{% for item in cart_items %}
<tr>
<td class="name">{{ item.name }}</td>
<td class="price">&yen;{{ item.unit_price }}</td>
<td>{{ item.amount }}</td>
<td class="price">&yen;{{ item.total_price }}</td>
<td>
<a href="" class="del">删除</a>
</td>
</tr>
{% endfor %}
<tr>
<td colspan="5" class="total price">&yen;{{ cart.total }}元</td>
</tr>
</table>
<a href="clear_cart" class="back">清空购物车</a>
{% else %}
<h3 style="clear: both;">购物车中暂时没有商品!</h3>
{% endif %}
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<style>
img { display: inline-block; width: 150px; height: 150px; border: 1px solid gray; }
th, td { margin: 0; padding: 0; width: 250px; text-align: left; }
.name { font-size: 14px; font-weight: bolder; }
.price { color: red; font-size: 18px; }
a { display: inline-block; width: 120px; height: 30px; line-height: 30px; text-align: center; background-color: red; }
a:link, a:visited { color: white; text-decoration: none; }
.left { float: left; width: 1000px;}
.right { float: right; }
</style>
</head>
<body>
<div class="left">
<h1>商品列表</h1>
<hr>
</div>
<div class="right">
<a href="/show_cart">查看购物车</a>
</div>
<table style="clear:both;">
<tr>
<th>商品名称</th>
<th>商品价格</th>
<th>商品图片</th>
<th>操作</th>
</tr>
{% for goods in goods_list %}
<tr>
<td class="name">{{ goods.name }}</td>
<td class="price">&yen;{{ goods.price }}</td>
<td>
<img src="{% static goods.image %}" alt="{{ goods.name }}">
</td>
<td>
<a href="/add_to_cart/{{ goods.id }}">加入购物车</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册