├── README.md ├── db.sqlite3 ├── ecommerce ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ ├── settings.cpython-37.pyc │ ├── urls.cpython-37.pyc │ └── wsgi.cpython-37.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ├── manage.py ├── static ├── 3D_printer.jfif ├── LED_Mirror.jpg ├── Sand_Table.jpg ├── code.jfif ├── css │ └── main.css ├── foosball_table.jfif ├── images │ ├── arrow-down.png │ ├── arrow-up.png │ ├── cart.png │ ├── placeholder.png │ └── up-arrow.png ├── js │ └── cart.js └── plotter.jfif └── store ├── __init__.py ├── __pycache__ ├── __init__.cpython-37.pyc ├── admin.cpython-37.pyc ├── apps.cpython-37.pyc ├── models.cpython-37.pyc ├── urls.cpython-37.pyc ├── utils.cpython-37.pyc └── views.cpython-37.pyc ├── admin.py ├── apps.py ├── migrations ├── 0001_initial.py ├── 0002_alter_product_price.py ├── __init__.py └── __pycache__ │ ├── 0001_initial.cpython-37.pyc │ ├── 0002_alter_product_price.cpython-37.pyc │ └── __init__.cpython-37.pyc ├── models.py ├── templates └── store │ ├── cart.html │ ├── checkout.html │ ├── main.html │ └── store.html ├── tests.py ├── urls.py ├── utils.py └── views.py /README.md: -------------------------------------------------------------------------------- 1 | # Build-an-e-commerce-store-with-Django 2 | Django is an extremely popular open-source Python-based web framework, designed to ease the creation of complex, database-driven websites with reusable pluggable components. Django has been famously used for sites such as Instagram, Mozilla, Disqus, and Clubhouse. In this workshop, we’ll deploy Django to create our own E-commerce storefront, which allows people to buy items with or without an account, combining the use of a database with cookies for anonymous usage. Aside from Django for the core functionality, we’ll use HTML/CSS/Javascript to improve our user experience as well as integrate with the PayPal API to handle purchase payments. 3 | 4 | Prerequisites: 5 | ✅ Visual Studio Code (https://code.visualstudio.com/download) 6 | ✅ Python (https://www.python.org/downloads) 7 | ✅ PayPal developer account (https://developer.paypal.com/docs/get-started/) 8 | 9 | ----------------------------------------- 10 | 11 | To learn more about The Assembly’s workshops, visit our website, social media or email us at workshops@theassembly.ae Our website: http://theassembly.ae Instagram: http://instagram.com/makesmartthings Facebook: http://fb.com/makesmartthings Twitter: http://twitter.com/makesmartthings #Django #PayPal #Python 12 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/db.sqlite3 -------------------------------------------------------------------------------- /ecommerce/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ecommerce/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/ecommerce/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /ecommerce/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/ecommerce/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /ecommerce/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/ecommerce/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /ecommerce/__pycache__/wsgi.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/ecommerce/__pycache__/wsgi.cpython-37.pyc -------------------------------------------------------------------------------- /ecommerce/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for ecommerce project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /ecommerce/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for ecommerce project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.2.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.2/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | import os 15 | 16 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 17 | BASE_DIR = Path(__file__).resolve().parent.parent 18 | 19 | 20 | # Quick-start development settings - unsuitable for production 21 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ 22 | 23 | # SECURITY WARNING: keep the secret key used in production secret! 24 | SECRET_KEY = 'django-insecure-kb*flvn$vlrwwa!!+5t!01ojq%k0k33t1uq+loerq(1_-&e*8p' 25 | 26 | # SECURITY WARNING: don't run with debug turned on in production! 27 | DEBUG = True 28 | 29 | ALLOWED_HOSTS = [] 30 | 31 | 32 | # Application definition 33 | 34 | INSTALLED_APPS = [ 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 41 | 'store.apps.StoreConfig', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'ecommerce.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 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 = 'ecommerce.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/3.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/3.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/3.2/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | 124 | STATICFILES_DIRS = [ 125 | os.path.join(BASE_DIR, 'static') 126 | ] 127 | 128 | MEDIA_ROOT = os.path.join(BASE_DIR, 'static') 129 | MEDIA_URL = '/images/' 130 | 131 | # Default primary key field type 132 | # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field 133 | 134 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 135 | -------------------------------------------------------------------------------- /ecommerce/urls.py: -------------------------------------------------------------------------------- 1 | """ecommerce URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.2/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 os import set_inheritable 17 | from django.contrib import admin 18 | from django.urls import path, include 19 | from django.conf.urls.static import static 20 | from django.conf import settings 21 | 22 | urlpatterns = [ 23 | path('admin/', admin.site.urls), 24 | path('', include('store.urls')), 25 | ] 26 | 27 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -------------------------------------------------------------------------------- /ecommerce/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for ecommerce 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/3.2/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', 'ecommerce.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /static/3D_printer.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/3D_printer.jfif -------------------------------------------------------------------------------- /static/LED_Mirror.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/LED_Mirror.jpg -------------------------------------------------------------------------------- /static/Sand_Table.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/Sand_Table.jpg -------------------------------------------------------------------------------- /static/code.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/code.jfif -------------------------------------------------------------------------------- /static/css/main.css: -------------------------------------------------------------------------------- 1 | body{ 2 | background-color: hsl(0, 0%, 98%); 3 | } 4 | 5 | h1,h2,h3,h4,h5,h6{ 6 | color:hsl(0, 0%, 30%); 7 | } 8 | 9 | .box-element{ 10 | box-shadow:hsl(0, 0%, 80%) 0 0 16px; 11 | background-color: #fff; 12 | border-radius: 4px; 13 | padding: 10px; 14 | } 15 | 16 | .thumbnail{ 17 | width: 100%; 18 | height: 200px; 19 | -webkit-box-shadow: -1px -3px 5px -2px rgba(214,214,214,1); 20 | -moz-box-shadow: -1px -3px 5px -2px rgba(214,214,214,1); 21 | box-shadow: -1px -3px 5px -2px rgba(214,214,214,1); 22 | } 23 | 24 | .product{ 25 | border-radius: 0 0 4px 4px; 26 | } 27 | 28 | #cart-icon{ 29 | width:25px; 30 | display: inline-block; 31 | margin-left: 15px; 32 | } 33 | 34 | #cart-total{ 35 | display: block; 36 | text-align: center; 37 | color:#fff; 38 | background-color: red; 39 | width: 20px; 40 | height: 25px; 41 | border-radius: 50%; 42 | font-size: 14px; 43 | } 44 | 45 | .col-lg-4, .col-lg-6, .col-lg-8, .col-lg-12{ 46 | margin-top: 10px; 47 | } 48 | 49 | .btn{ 50 | border-radius: 0; 51 | } 52 | 53 | .row-image{ 54 | width: 100px; 55 | } 56 | 57 | .form-field{ 58 | width:250px; 59 | display: inline-block; 60 | padding: 5px; 61 | } 62 | 63 | .cart-row{ 64 | display: flex; 65 | align-items: flex-stretch; 66 | padding-bottom: 10px; 67 | margin-bottom: 10px; 68 | border-bottom: 1px solid #ececec; 69 | 70 | } 71 | 72 | .quantity{ 73 | display: inline-block; 74 | font-weight: 700; 75 | padding-right:10px; 76 | 77 | 78 | } 79 | 80 | .chg-quantity{ 81 | width: 12px; 82 | cursor: pointer; 83 | display: block; 84 | margin-top: 5px; 85 | transition:.1s; 86 | } 87 | 88 | .chg-quantity:hover{ 89 | opacity: .6; 90 | } 91 | 92 | 93 | .hidden{ 94 | display: none!important; 95 | } -------------------------------------------------------------------------------- /static/foosball_table.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/foosball_table.jfif -------------------------------------------------------------------------------- /static/images/arrow-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/images/arrow-down.png -------------------------------------------------------------------------------- /static/images/arrow-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/images/arrow-up.png -------------------------------------------------------------------------------- /static/images/cart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/images/cart.png -------------------------------------------------------------------------------- /static/images/placeholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/images/placeholder.png -------------------------------------------------------------------------------- /static/images/up-arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/images/up-arrow.png -------------------------------------------------------------------------------- /static/js/cart.js: -------------------------------------------------------------------------------- 1 | var updateBtns = document.getElementsByClassName('update-cart') 2 | 3 | for (i = 0; i < updateBtns.length; i++) { 4 | updateBtns[i].addEventListener('click', function(){ 5 | var productId = this.dataset.product 6 | var action = this.dataset.action 7 | console.log('productId:', productId, 'Action:', action) 8 | console.log('USER:', user) 9 | 10 | if (user == 'AnonymousUser'){ 11 | addCookieItem(productId, action) 12 | }else{ 13 | updateUserOrder(productId, action) 14 | } 15 | }) 16 | } 17 | 18 | function updateUserOrder(productId, action){ 19 | console.log('User is authenticated, sending data...') 20 | 21 | var url = '/update_item/' 22 | 23 | fetch(url, { 24 | method:'POST', 25 | headers:{ 26 | 'Content-Type':'application/json', 27 | 'X-CSRFToken':csrftoken, 28 | }, 29 | body:JSON.stringify({'productId':productId, 'action':action}) 30 | }) 31 | .then((response) => { 32 | return response.json(); 33 | }) 34 | .then((data) => { 35 | location.reload() 36 | }); 37 | } 38 | 39 | function addCookieItem(productId, action){ 40 | console.log('User is not authenticated') 41 | 42 | if (action == 'add'){ 43 | if (cart[productId] == undefined){ 44 | cart[productId] = {'quantity':1} 45 | 46 | }else{ 47 | cart[productId]['quantity'] += 1 48 | } 49 | } 50 | 51 | if (action == 'remove'){ 52 | cart[productId]['quantity'] -= 1 53 | 54 | if (cart[productId]['quantity'] <= 0){ 55 | console.log('Item should be deleted') 56 | delete cart[productId]; 57 | } 58 | } 59 | console.log('CART:', cart) 60 | document.cookie ='cart=' + JSON.stringify(cart) + ";domain=;path=/" 61 | 62 | location.reload() 63 | } -------------------------------------------------------------------------------- /static/plotter.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/static/plotter.jfif -------------------------------------------------------------------------------- /store/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /store/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/admin.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/admin.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/apps.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/apps.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/models.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/models.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /store/__pycache__/views.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/__pycache__/views.cpython-37.pyc -------------------------------------------------------------------------------- /store/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import * 3 | 4 | admin.site.register(Customer) 5 | admin.site.register(Product) 6 | admin.site.register(Order) 7 | admin.site.register(OrderItem) 8 | admin.site.register(ShippingAddress) -------------------------------------------------------------------------------- /store/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class StoreConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'store' 7 | -------------------------------------------------------------------------------- /store/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.5 on 2021-08-04 14:24 2 | 3 | from django.conf import settings 4 | from django.db import migrations, models 5 | import django.db.models.deletion 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | initial = True 11 | 12 | dependencies = [ 13 | migrations.swappable_dependency(settings.AUTH_USER_MODEL), 14 | ] 15 | 16 | operations = [ 17 | migrations.CreateModel( 18 | name='Customer', 19 | fields=[ 20 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 21 | ('name', models.CharField(max_length=200, null=True)), 22 | ('email', models.CharField(max_length=200)), 23 | ('user', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), 24 | ], 25 | ), 26 | migrations.CreateModel( 27 | name='Order', 28 | fields=[ 29 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 30 | ('date_ordered', models.DateTimeField(auto_now_add=True)), 31 | ('complete', models.BooleanField(default=False)), 32 | ('transaction_id', models.CharField(max_length=100, null=True)), 33 | ('customer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='store.customer')), 34 | ], 35 | ), 36 | migrations.CreateModel( 37 | name='Product', 38 | fields=[ 39 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 40 | ('name', models.CharField(max_length=200)), 41 | ('price', models.FloatField()), 42 | ('digital', models.BooleanField(blank=True, default=False, null=True)), 43 | ('image', models.ImageField(blank=True, null=True, upload_to='')), 44 | ], 45 | ), 46 | migrations.CreateModel( 47 | name='ShippingAddress', 48 | fields=[ 49 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 50 | ('address', models.CharField(max_length=200)), 51 | ('city', models.CharField(max_length=200)), 52 | ('state', models.CharField(max_length=200)), 53 | ('zipcode', models.CharField(max_length=200)), 54 | ('date_added', models.DateTimeField(auto_now_add=True)), 55 | ('customer', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='store.customer')), 56 | ('order', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='store.order')), 57 | ], 58 | ), 59 | migrations.CreateModel( 60 | name='OrderItem', 61 | fields=[ 62 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 63 | ('quantity', models.IntegerField(blank=True, default=0, null=True)), 64 | ('date_added', models.DateTimeField(auto_now_add=True)), 65 | ('order', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='store.order')), 66 | ('product', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='store.product')), 67 | ], 68 | ), 69 | ] 70 | -------------------------------------------------------------------------------- /store/migrations/0002_alter_product_price.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.5 on 2021-08-04 18:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('store', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='product', 15 | name='price', 16 | field=models.DecimalField(decimal_places=2, max_digits=7), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /store/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /store/migrations/__pycache__/0001_initial.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/migrations/__pycache__/0001_initial.cpython-37.pyc -------------------------------------------------------------------------------- /store/migrations/__pycache__/0002_alter_product_price.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/migrations/__pycache__/0002_alter_product_price.cpython-37.pyc -------------------------------------------------------------------------------- /store/migrations/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/The-Assembly/Build-an-e-commerce-store-with-Django/1d0ae821ad86ef19441377262030909c784e13b3/store/migrations/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /store/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.contrib.auth.models import User 3 | 4 | # Create your models here. 5 | 6 | class Customer(models.Model): 7 | user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE) 8 | name = models.CharField(max_length=200, null=True) 9 | email = models.CharField(max_length=200) 10 | 11 | def __str__(self): 12 | return self.name 13 | 14 | class Product(models.Model): 15 | name = models.CharField(max_length=200) 16 | price = models.DecimalField(max_digits=7, decimal_places=2) 17 | digital = models.BooleanField(default=False,null=True, blank=True) 18 | image = models.ImageField(null=True, blank=True) 19 | 20 | def __str__(self): 21 | return self.name 22 | 23 | @property 24 | def imageURL(self): 25 | try: 26 | url = self.image.url 27 | except: 28 | url = '' 29 | return url 30 | 31 | class Order(models.Model): 32 | customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True) 33 | date_ordered = models.DateTimeField(auto_now_add=True) 34 | complete = models.BooleanField(default=False) 35 | transaction_id = models.CharField(max_length=100, null=True) 36 | 37 | def __str__(self): 38 | return str(self.id) 39 | 40 | @property 41 | def get_cart_total(self): 42 | orderitems = self.orderitem_set.all() 43 | total = sum([item.get_total for item in orderitems]) 44 | return total 45 | 46 | @property 47 | def get_cart_items(self): 48 | orderitems = self.orderitem_set.all() 49 | total = sum([item.quantity for item in orderitems]) 50 | return total 51 | 52 | @property 53 | def shipping(self): 54 | shipping = False 55 | orderitems = self.orderitem_set.all() 56 | for i in orderitems: 57 | if i.product.digital == False: 58 | shipping = True 59 | return shipping 60 | 61 | class OrderItem(models.Model): 62 | product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True) 63 | order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True) 64 | quantity = models.IntegerField(default=0, null=True, blank=True) 65 | date_added = models.DateTimeField(auto_now_add=True) 66 | 67 | @property 68 | def get_total(self): 69 | total = self.product.price * self.quantity 70 | return total 71 | 72 | 73 | class ShippingAddress(models.Model): 74 | customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True) 75 | order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True) 76 | address = models.CharField(max_length=200, null=False) 77 | city = models.CharField(max_length=200, null=False) 78 | state = models.CharField(max_length=200, null=False) 79 | zipcode = models.CharField(max_length=200, null=False) 80 | date_added = models.DateTimeField(auto_now_add=True) 81 | 82 | def __str__(self): 83 | return self.address -------------------------------------------------------------------------------- /store/templates/store/cart.html: -------------------------------------------------------------------------------- 1 | {% extends 'store/main.html' %} 2 | {% load static %} 3 | {% block content %} 4 |
Items: {{order.get_cart_items}} |
15 | Total:${{order.get_cart_total}} |
16 | 17 | Checkout 18 | | 19 |
---|
{{item.product.name}}
${{item.product.price|floatformat:2}}
{{item.quantity}}
41 |${{item.get_total}}
{{item.product.name}}
${{item.product.price|floatformat:2}}
x{{item.quantity}}