diff --git a/Day31-Day35/shop/cart/__init__.py b/Day31-Day35/shop/cart/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Day31-Day35/shop/cart/admin.py b/Day31-Day35/shop/cart/admin.py new file mode 100644 index 0000000..33eb176 --- /dev/null +++ b/Day31-Day35/shop/cart/admin.py @@ -0,0 +1,11 @@ +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) diff --git a/Day31-Day35/shop/cart/apps.py b/Day31-Day35/shop/cart/apps.py new file mode 100644 index 0000000..7cc6ec1 --- /dev/null +++ b/Day31-Day35/shop/cart/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CartConfig(AppConfig): + name = 'cart' diff --git a/Day31-Day35/shop/cart/migrations/0001_initial.py b/Day31-Day35/shop/cart/migrations/0001_initial.py new file mode 100644 index 0000000..a3c1f87 --- /dev/null +++ b/Day31-Day35/shop/cart/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# 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',), + }, + ), + ] diff --git a/Day31-Day35/shop/cart/migrations/__init__.py b/Day31-Day35/shop/cart/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Day31-Day35/shop/cart/models.py b/Day31-Day35/shop/cart/models.py new file mode 100644 index 0000000..5f6a9b8 --- /dev/null +++ b/Day31-Day35/shop/cart/models.py @@ -0,0 +1,13 @@ +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',) diff --git a/Day31-Day35/shop/cart/tests.py b/Day31-Day35/shop/cart/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/Day31-Day35/shop/cart/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Day31-Day35/shop/cart/views.py b/Day31-Day35/shop/cart/views.py new file mode 100644 index 0000000..a1c3378 --- /dev/null +++ b/Day31-Day35/shop/cart/views.py @@ -0,0 +1,16 @@ +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 diff --git a/Day31-Day35/shop/db.sqlite3 b/Day31-Day35/shop/db.sqlite3 new file mode 100644 index 0000000..cb7d39a Binary files /dev/null and b/Day31-Day35/shop/db.sqlite3 differ diff --git a/Day31-Day35/shop/manage.py b/Day31-Day35/shop/manage.py new file mode 100755 index 0000000..175f50a --- /dev/null +++ b/Day31-Day35/shop/manage.py @@ -0,0 +1,15 @@ +#!/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) diff --git a/Day31-Day35/shop/shop/__init__.py b/Day31-Day35/shop/shop/__init__.py new file mode 100644 index 0000000..aa60bed --- /dev/null +++ b/Day31-Day35/shop/shop/__init__.py @@ -0,0 +1,3 @@ +import pymysql + +pymysql.install_as_MySQLdb() \ No newline at end of file diff --git a/Day31-Day35/shop/shop/settings.py b/Day31-Day35/shop/shop/settings.py new file mode 100644 index 0000000..337b099 --- /dev/null +++ b/Day31-Day35/shop/shop/settings.py @@ -0,0 +1,126 @@ +""" +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/' diff --git a/Day31-Day35/shop/shop/urls.py b/Day31-Day35/shop/shop/urls.py new file mode 100644 index 0000000..fb34b04 --- /dev/null +++ b/Day31-Day35/shop/shop/urls.py @@ -0,0 +1,26 @@ +"""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/', views.add_to_cart), + path('admin/', admin.site.urls), +] diff --git a/Day31-Day35/shop/shop/wsgi.py b/Day31-Day35/shop/shop/wsgi.py new file mode 100644 index 0000000..1aa46d9 --- /dev/null +++ b/Day31-Day35/shop/shop/wsgi.py @@ -0,0 +1,16 @@ +""" +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() diff --git a/Day31-Day35/shop/shop_create_sql.sql b/Day31-Day35/shop/shop_create_sql.sql new file mode 100644 index 0000000..4a6ed9d --- /dev/null +++ b/Day31-Day35/shop/shop_create_sql.sql @@ -0,0 +1,18 @@ +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 diff --git a/Day31-Day35/shop/static/images/dolbee.jpg b/Day31-Day35/shop/static/images/dolbee.jpg new file mode 100644 index 0000000..03e4a92 Binary files /dev/null and b/Day31-Day35/shop/static/images/dolbee.jpg differ diff --git a/Day31-Day35/shop/static/images/lay.jpg b/Day31-Day35/shop/static/images/lay.jpg new file mode 100644 index 0000000..254a4cc Binary files /dev/null and b/Day31-Day35/shop/static/images/lay.jpg differ diff --git a/Day31-Day35/shop/static/images/noodle.jpg b/Day31-Day35/shop/static/images/noodle.jpg new file mode 100644 index 0000000..1907c18 Binary files /dev/null and b/Day31-Day35/shop/static/images/noodle.jpg differ diff --git a/Day31-Day35/shop/static/images/oil.jpg b/Day31-Day35/shop/static/images/oil.jpg new file mode 100644 index 0000000..bcb3f7a Binary files /dev/null and b/Day31-Day35/shop/static/images/oil.jpg differ diff --git a/Day31-Day35/shop/static/images/wang.jpg b/Day31-Day35/shop/static/images/wang.jpg new file mode 100644 index 0000000..8b94ca2 Binary files /dev/null and b/Day31-Day35/shop/static/images/wang.jpg differ diff --git a/Day31-Day35/shop/static/images/wine.jpg b/Day31-Day35/shop/static/images/wine.jpg new file mode 100644 index 0000000..96e1a56 Binary files /dev/null and b/Day31-Day35/shop/static/images/wine.jpg differ diff --git a/Day31-Day35/shop/templates/cart.html b/Day31-Day35/shop/templates/cart.html new file mode 100644 index 0000000..2291b9c --- /dev/null +++ b/Day31-Day35/shop/templates/cart.html @@ -0,0 +1,55 @@ + + + + + + + +
+

购物车列表

+
+
+
+ 返回 +
+ {% if cart_items %} + + + + + + + + + {% for item in cart_items %} + + + + + + + + {% endfor %} + + + +
商品名称商品单价商品数量商品总价操作
{{ item.name }}¥{{ item.unit_price }}{{ item.amount }}¥{{ item.total_price }} + 删除 +
¥{{ cart.total }}元
+ 清空购物车 + {% else %} +

购物车中暂时没有商品!

+ {% endif %} + + \ No newline at end of file diff --git a/Day31-Day35/shop/templates/goods.html b/Day31-Day35/shop/templates/goods.html new file mode 100644 index 0000000..9e3c9b7 --- /dev/null +++ b/Day31-Day35/shop/templates/goods.html @@ -0,0 +1,46 @@ + +{% load staticfiles %} + + + + + + +
+

商品列表

+
+
+
+ 查看购物车 +
+ + + + + + + + {% for goods in goods_list %} + + + + + + + {% endfor %} +
商品名称商品价格商品图片操作
{{ goods.name }}¥{{ goods.price }} + {{ goods.name }} + + 加入购物车 +
+ + \ No newline at end of file