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

添加了购物车案例

上级 70b6b6e2
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'),
(default, '多儿比(Dolbee)黄桃水果罐头', 6.8, 'images/dolbee.jpg'),
(default, '王致和 精制料酒 500ml', 7.9, 'images/wine.jpg'),
(default, '陈克明 面条 鸡蛋龙须挂面', 1.0, 'images/noodle.jpg'),
(default, '鲁花 菜籽油 4L', 69.9, 'images/oil.jpg');
\ No newline at end of file
<!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.
先完成此消息的编辑!
想要评论请 注册