Skip to content

Commit b2b0fa7

Browse files
committed
添加了购物车案例
1 parent 70b6b6e commit b2b0fa7

23 files changed

+380
-0
lines changed

Day31-Day35/shop/cart/__init__.py

Whitespace-only changes.

Day31-Day35/shop/cart/admin.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.contrib import admin
2+
3+
from cart.models import Goods
4+
5+
6+
class GoodsAdmin(admin.ModelAdmin):
7+
8+
list_display = ('id', 'name', 'price', 'image')
9+
10+
11+
admin.site.register(Goods, GoodsAdmin)

Day31-Day35/shop/cart/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CartConfig(AppConfig):
5+
name = 'cart'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 2.0.5 on 2018-05-25 05:11
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Goods',
16+
fields=[
17+
('id', models.AutoField(db_column='gid', primary_key=True, serialize=False)),
18+
('name', models.CharField(db_column='gname', max_length=50)),
19+
('price', models.DecimalField(db_column='gprice', decimal_places=2, max_digits=10)),
20+
('image', models.CharField(db_column='gimage', max_length=255)),
21+
],
22+
options={
23+
'db_table': 'tb_goods',
24+
'ordering': ('id',),
25+
},
26+
),
27+
]

Day31-Day35/shop/cart/migrations/__init__.py

Whitespace-only changes.

Day31-Day35/shop/cart/models.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.db import models
2+
3+
4+
class Goods(models.Model):
5+
6+
id = models.AutoField(primary_key=True, db_column='gid')
7+
name = models.CharField(max_length=50, db_column='gname')
8+
price = models.DecimalField(max_digits=10, decimal_places=2, db_column='gprice')
9+
image = models.CharField(max_length=255, db_column='gimage')
10+
11+
class Meta:
12+
db_table = 'tb_goods'
13+
ordering = ('id',)

Day31-Day35/shop/cart/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

Day31-Day35/shop/cart/views.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.shortcuts import render
2+
3+
from cart.models import Goods
4+
5+
6+
def index(request):
7+
goods_list = list(Goods.objects.all())
8+
return render(request, 'goods.html', {'goods_list': goods_list})
9+
10+
11+
def show_cart(request):
12+
return render(request, 'cart.html')
13+
14+
15+
def add_to_cart(request, no):
16+
pass

Day31-Day35/shop/db.sqlite3

132 KB
Binary file not shown.

Day31-Day35/shop/manage.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shop.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

Day31-Day35/shop/shop/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pymysql
2+
3+
pymysql.install_as_MySQLdb()

Day31-Day35/shop/shop/settings.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
Django settings for shop project.
3+
4+
Generated by 'django-admin startproject' using Django 2.0.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '+gqc54!5+uhvc^o0)fjvihmg&5uu^u+#s5m*fc+e+@bw*(+!w*'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'cart',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'shop.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [os.path.join(BASE_DIR, 'templates')]
59+
,
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'shop.wsgi.application'
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
77+
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.mysql',
81+
'NAME': 'Shop',
82+
'HOST': 'localhost',
83+
'PORT': 3306,
84+
'USER': 'root',
85+
'PASSWORD': '123456',
86+
}
87+
}
88+
89+
90+
# Password validation
91+
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
92+
93+
AUTH_PASSWORD_VALIDATORS = [
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
102+
},
103+
{
104+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
105+
},
106+
]
107+
108+
109+
# Internationalization
110+
# https://docs.djangoproject.com/en/2.0/topics/i18n/
111+
112+
LANGUAGE_CODE = 'en-us'
113+
114+
TIME_ZONE = 'Asia/Chongqing'
115+
116+
USE_I18N = True
117+
118+
USE_L10N = True
119+
120+
USE_TZ = True
121+
122+
123+
# Static files (CSS, JavaScript, Images)
124+
# https://docs.djangoproject.com/en/2.0/howto/static-files/
125+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
126+
STATIC_URL = '/static/'

Day31-Day35/shop/shop/urls.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""shop URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
from cart import views
20+
21+
urlpatterns = [
22+
path('', views.index),
23+
path('show_cart', views.show_cart),
24+
path('add_to_cart/<int:no>', views.add_to_cart),
25+
path('admin/', admin.site.urls),
26+
]

Day31-Day35/shop/shop/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for shop project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shop.settings")
15+
16+
application = get_wsgi_application()

Day31-Day35/shop/shop_create_sql.sql

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
drop database if exists Shop;
2+
create database Shop default charset utf8;
3+
use Shop;
4+
create table tb_goods
5+
(
6+
gid int not null auto_increment,
7+
gname varchar(50) not null,
8+
gprice decimal(10,2) not null,
9+
gimage varchar(255),
10+
primary key (gid)
11+
);
12+
insert into tb_goods values
13+
(default, '乐事(Lay’s)无限薯片', 8.2, 'images/lay.jpg'),
14+
(default, '旺旺 仙贝 加量装 540g', 18.5, 'images/wang.jpg'),
15+
(default, '多儿比(Dolbee)黄桃水果罐头', 6.8, 'images/dolbee.jpg'),
16+
(default, '王致和 精制料酒 500ml', 7.9, 'images/wine.jpg'),
17+
(default, '陈克明 面条 鸡蛋龙须挂面', 1.0, 'images/noodle.jpg'),
18+
(default, '鲁花 菜籽油 4L', 69.9, 'images/oil.jpg');
46.4 KB
Loading
13.3 KB
Loading
22.8 KB
Loading
32.3 KB
Loading
32.4 KB
Loading
10.2 KB
Loading

Day31-Day35/shop/templates/cart.html

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<style>
6+
th, td { margin: 0; padding: 0; width: 180px; text-align: left; }
7+
.name { font-size: 14px; font-weight: bolder; width: 280px; }
8+
.price { color: red; font-size: 18px; }
9+
a { display: inline-block; text-align: center; background-color: red; }
10+
.back { width: 120px; height: 30px; line-height: 30px; }
11+
.del { width: 60px; height: 20px; line-height: 20px; }
12+
a:link, a:visited { color: white; text-decoration: none; }
13+
.left { float: left; width: 1000px;}
14+
.right { float: right; }
15+
.total { text-align: right; }
16+
</style>
17+
</head>
18+
<body>
19+
<div class="left">
20+
<h1>购物车列表</h1>
21+
<hr>
22+
</div>
23+
<div class="right">
24+
<a href="list_goods" class="back">返回</a>
25+
</div>
26+
{% if cart_items %}
27+
<table style="clear: both;">
28+
<tr>
29+
<th>商品名称</th>
30+
<th>商品单价</th>
31+
<th>商品数量</th>
32+
<th>商品总价</th>
33+
<th>操作</th>
34+
</tr>
35+
{% for item in cart_items %}
36+
<tr>
37+
<td class="name">{{ item.name }}</td>
38+
<td class="price">&yen;{{ item.unit_price }}</td>
39+
<td>{{ item.amount }}</td>
40+
<td class="price">&yen;{{ item.total_price }}</td>
41+
<td>
42+
<a href="" class="del">删除</a>
43+
</td>
44+
</tr>
45+
{% endfor %}
46+
<tr>
47+
<td colspan="5" class="total price">&yen;{{ cart.total }}元</td>
48+
</tr>
49+
</table>
50+
<a href="clear_cart" class="back">清空购物车</a>
51+
{% else %}
52+
<h3 style="clear: both;">购物车中暂时没有商品!</h3>
53+
{% endif %}
54+
</body>
55+
</html>

Day31-Day35/shop/templates/goods.html

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
{% load staticfiles %}
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
img { display: inline-block; width: 150px; height: 150px; border: 1px solid gray; }
8+
th, td { margin: 0; padding: 0; width: 250px; text-align: left; }
9+
.name { font-size: 14px; font-weight: bolder; }
10+
.price { color: red; font-size: 18px; }
11+
a { display: inline-block; width: 120px; height: 30px; line-height: 30px; text-align: center; background-color: red; }
12+
a:link, a:visited { color: white; text-decoration: none; }
13+
.left { float: left; width: 1000px;}
14+
.right { float: right; }
15+
</style>
16+
</head>
17+
<body>
18+
<div class="left">
19+
<h1>商品列表</h1>
20+
<hr>
21+
</div>
22+
<div class="right">
23+
<a href="/show_cart">查看购物车</a>
24+
</div>
25+
<table style="clear:both;">
26+
<tr>
27+
<th>商品名称</th>
28+
<th>商品价格</th>
29+
<th>商品图片</th>
30+
<th>操作</th>
31+
</tr>
32+
{% for goods in goods_list %}
33+
<tr>
34+
<td class="name">{{ goods.name }}</td>
35+
<td class="price">&yen;{{ goods.price }}</td>
36+
<td>
37+
<img src="{% static goods.image %}" alt="{{ goods.name }}">
38+
</td>
39+
<td>
40+
<a href="/add_to_cart/{{ goods.id }}">加入购物车</a>
41+
</td>
42+
</tr>
43+
{% endfor %}
44+
</table>
45+
</body>
46+
</html>

0 commit comments

Comments
 (0)