├── .DS_Store ├── .gitignore ├── .vscode └── settings.json ├── README.md ├── socar ├── .DS_Store ├── db.sqlite3 ├── manage.py ├── restfulshop │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ └── views.py ├── shop │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── forms.py │ ├── middles.py │ ├── migrations │ │ ├── 0001_initial.py │ │ └── __init__.py │ ├── models.py │ ├── static │ │ └── media │ │ │ ├── imgs │ │ │ ├── 640.jpg │ │ │ └── 下载.jpeg │ │ │ └── upload │ │ │ └── 2018 │ │ │ └── 05 │ │ │ └── 22 │ │ │ └── 8c1001e93901213fab66256a58e736d12f2e952a.jpg │ ├── templates │ │ ├── base.html │ │ ├── buy.html │ │ ├── carinfo.html │ │ ├── create.html │ │ ├── foot.html │ │ ├── index.html │ │ ├── info.html │ │ ├── login.html │ │ ├── nav.html │ │ ├── perinfo.html │ │ └── tmp.html │ ├── templatetags │ │ ├── __init__.py │ │ └── upper.py │ ├── tests.py │ ├── urls.py │ └── views.py └── socar │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── tests └── .gitkeep /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | .static_storage/ 56 | .media/ 57 | local_settings.py 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.pythonPath": "/Users/xiaofang/envs/py3/bin/python" 3 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 基于Django的商城购物系统 2 | 3 | ## 项目环境 4 | 5 | - Python 3.x 6 | - Django 2.0.x 7 | 8 | -------------------------------------------------------------------------------- /socar/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/.DS_Store -------------------------------------------------------------------------------- /socar/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/db.sqlite3 -------------------------------------------------------------------------------- /socar/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "socar.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) 16 | -------------------------------------------------------------------------------- /socar/restfulshop/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/restfulshop/__init__.py -------------------------------------------------------------------------------- /socar/restfulshop/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /socar/restfulshop/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class RestfulshopConfig(AppConfig): 5 | name = 'restfulshop' 6 | -------------------------------------------------------------------------------- /socar/restfulshop/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/restfulshop/migrations/__init__.py -------------------------------------------------------------------------------- /socar/restfulshop/models.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from shop.models import Commodity 3 | 4 | 5 | class CSerializers(serializers.ModelSerializer): 6 | class Meta: 7 | model = Commodity 8 | fields = ('id', 'name', 'price', 'discount', 'status') -------------------------------------------------------------------------------- /socar/restfulshop/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /socar/restfulshop/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | from rest_framework import routers 3 | from .views import goods, good, CommoditysView, CommodityView 4 | 5 | router = routers.DefaultRouter() 6 | 7 | # router.register('goods', CommodityView) 8 | 9 | # print(router.registry) 10 | # Wire up our API using automatic URL routing. 11 | # Additionally, we include login URLs for the browsable API. 12 | urlpatterns = [ 13 | # path(r'', include(router.urls)), 14 | # path(r'auth', include('rest_framework.urls', namespace='rest_framework')) 15 | path("goods/", CommoditysView.as_view()), 16 | path("", include(router.urls)), 17 | path("goods/", CommodityView.as_view()) 18 | ] -------------------------------------------------------------------------------- /socar/restfulshop/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework import viewsets 3 | from shop.models import Commodity 4 | from .models import CSerializers 5 | from rest_framework.decorators import api_view, permission_classes 6 | from rest_framework.response import Response 7 | from rest_framework import permissions 8 | from django.urls import path 9 | from rest_framework import status 10 | from rest_framework import generics, mixins 11 | from rest_framework.response import Response 12 | 13 | # Create your views here. 14 | ####################### 15 | # 基于类 16 | ###################### 17 | class CommoditysView(generics.ListCreateAPIView): 18 | queryset = Commodity.objects.all() 19 | serializer_class = CSerializers 20 | 21 | 22 | class CommodityView(generics.ListCreateAPIView): 23 | queryset = Commodity.objects.all() 24 | serializer_class = CSerializers 25 | 26 | def get(self, request, *args, **kwargs): 27 | try: 28 | kid = kwargs.get("id") 29 | # result = Commodity.objetcs.get_or_404(id=kid) 30 | result = self.queryset.get(id=kid) 31 | ser = CSerializers(result) 32 | return Response(ser.data, status.HTTP_200_OK) 33 | except: 34 | return Response([], status.HTTP_404_NOT_FOUND) 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ####################### 43 | # 基于函数 44 | ###################### 45 | @api_view(["GET", "POST"]) 46 | @permission_classes((permissions.AllowAny,)) 47 | def goods(request): 48 | if request.method == "GET": 49 | result = Commodity.objects.all() 50 | ser = CSerializers(result, many=True) 51 | return Response(ser.data, 200) 52 | 53 | if request.method == "POST": 54 | ser = CSerializers(data=request.data) 55 | if ser.is_valid(): 56 | print(ser.data) 57 | ser.create(ser.validated_data) 58 | # ser.save() 59 | 60 | return Response(ser.data, 200) 61 | 62 | 63 | @api_view(["GET", "DELETE"]) 64 | @permission_classes((permissions.AllowAny,)) 65 | def good(request, id): 66 | try: 67 | g = Commodity.objects.get(id=id) 68 | 69 | ser = CSerializers(g) 70 | data = ser.data 71 | if request.method == "DELETE": 72 | 73 | g.delete() 74 | 75 | return Response(data, 200) 76 | except Exception as e: 77 | return Response([{'error':'notexzit'}], status.HTTP_204_NO_CONTENT) 78 | 79 | 80 | -------------------------------------------------------------------------------- /socar/shop/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/__init__.py -------------------------------------------------------------------------------- /socar/shop/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Commodity, Nuser 3 | # from django.contrib.auth.models import User 4 | 5 | # Register your models here. 6 | 7 | 8 | class CommodityAdmin(admin.ModelAdmin): 9 | list_display = ('name', 'price', 'status', 'ptime') 10 | # fields = ('id', 'name', 'price', 'status') 11 | search_fields = ('name', 'ptime', 'status') 12 | list_filter = ( 13 | 'ptime', 14 | 'status', 15 | ) 16 | fieldsets = ( 17 | (None, { 18 | 'fields': ( 19 | 'name', 20 | 'price', 21 | 'discount', 22 | 'status', 23 | 'img', 24 | 'context' 25 | ) 26 | }), 27 | ) 28 | 29 | 30 | 31 | admin.site.register(Commodity, CommodityAdmin) 32 | # admin.site.unregister(User) 33 | admin.site.register(Nuser) 34 | -------------------------------------------------------------------------------- /socar/shop/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ShopConfig(AppConfig): 5 | name = 'shop' 6 | -------------------------------------------------------------------------------- /socar/shop/forms.py: -------------------------------------------------------------------------------- 1 | # from django.contrib.auth.forms import UserCreationForm 2 | # from django.contrib.auth.forms import AuthenticationForm 3 | from django import forms 4 | # from .models import MyUser 5 | 6 | 7 | # class UserForm(forms.ModelForm): 8 | # class Meta: 9 | # model = MyUser 10 | # fields = ['username', "passwd1"] 11 | 12 | 13 | # class UserRegist(forms.ModelForm): 14 | # class Meta: 15 | # model = MyUser 16 | # fields = ['username', 'passwd1', 'passwd2', 'nick'] 17 | 18 | # class CreateUser(UserCreationForm): 19 | # pass 20 | 21 | 22 | class MyForm(forms.Form): 23 | tel = forms.CharField(label="电话号码",max_length=12) 24 | 25 | def clean(self): 26 | tel = self.cleaned_data.get("tel") 27 | print(tel) 28 | if tel == "15732633601": 29 | print("success") 30 | return self.cleaned_data 31 | else: 32 | raise forms.ValidationError("电话号码有误") 33 | 34 | 35 | class BuyForm(forms.Form): 36 | # 购买界面的表单 37 | num = forms.IntegerField(label="数量", max_value=10, min_value=0) 38 | colour = forms.ChoiceField(label="颜色", choices=( 39 | (1, "黑色"), 40 | (2, "红色"), 41 | (3, "紫色"), 42 | )) 43 | size = forms.ChoiceField(label="尺码", choices=( 44 | (1, "M"), 45 | (2, "L"), 46 | (3, "XL"), 47 | )) -------------------------------------------------------------------------------- /socar/shop/middles.py: -------------------------------------------------------------------------------- 1 | # 自定义中间件 2 | from django.utils.deprecation import MiddlewareMixin 3 | 4 | class ShopCar(MiddlewareMixin): 5 | 6 | # def __init__(self, get_response): 7 | # self.get_response = get_response 8 | 9 | # def __call__(self, request): 10 | # return self.get_response(request) 11 | 12 | def process_request(self, request): 13 | # 读取cookies 14 | # 获取cookies,并给request添加新的属性 15 | # print(dir(request.user)) 16 | # 购物车需要和用户绑定 17 | request.user.car = {} 18 | shopcar = request.COOKIES.get("goods") 19 | if shopcar: 20 | 21 | request.user.car.update(eval(shopcar)) 22 | 23 | 24 | def process_response(self, request, response): 25 | # 获取request的car属性,并dump到cookies 26 | if not hasattr(request.user, "car"): 27 | request.user.car = {} 28 | response.set_cookie("goods", request.user.car) 29 | return response 30 | -------------------------------------------------------------------------------- /socar/shop/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.0.5 on 2018-06-06 02:51 2 | 3 | import ckeditor_uploader.fields 4 | import django.contrib.auth.models 5 | import django.contrib.auth.validators 6 | from django.db import migrations, models 7 | import django.utils.timezone 8 | 9 | 10 | class Migration(migrations.Migration): 11 | 12 | initial = True 13 | 14 | dependencies = [ 15 | ('auth', '0009_alter_user_last_name_max_length'), 16 | ] 17 | 18 | operations = [ 19 | migrations.CreateModel( 20 | name='Nuser', 21 | fields=[ 22 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 23 | ('password', models.CharField(max_length=128, verbose_name='password')), 24 | ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), 25 | ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), 26 | ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), 27 | ('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')), 28 | ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), 29 | ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), 30 | ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), 31 | ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), 32 | ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), 33 | ('desc', models.TextField()), 34 | ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), 35 | ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), 36 | ], 37 | options={ 38 | 'verbose_name': 'user', 39 | 'verbose_name_plural': 'users', 40 | 'abstract': False, 41 | }, 42 | managers=[ 43 | ('objects', django.contrib.auth.models.UserManager()), 44 | ], 45 | ), 46 | migrations.CreateModel( 47 | name='Commodity', 48 | fields=[ 49 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 50 | ('name', models.CharField(max_length=255, verbose_name='商品名称')), 51 | ('price', models.FloatField(verbose_name='商品价格')), 52 | ('discount', models.FloatField(verbose_name='商品折扣')), 53 | ('status', models.IntegerField(choices=[(1, '在售'), (2, '缺货'), (3, '下架')], verbose_name='商品状态')), 54 | ('ptime', models.DateTimeField(auto_now=True, verbose_name='上架时间')), 55 | ('context', ckeditor_uploader.fields.RichTextUploadingField(null=True)), 56 | ('img', models.ImageField(upload_to='imgs/', verbose_name='商品图片')), 57 | ], 58 | options={ 59 | 'verbose_name': '商品信息', 60 | 'verbose_name_plural': '商品信息', 61 | }, 62 | ), 63 | ] 64 | -------------------------------------------------------------------------------- /socar/shop/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/migrations/__init__.py -------------------------------------------------------------------------------- /socar/shop/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from ckeditor_uploader.fields import RichTextUploadingField 3 | from django.contrib.auth.models import AbstractUser 4 | 5 | # Create your models here. 6 | 7 | 8 | class Nuser(AbstractUser): 9 | desc = models.TextField() 10 | 11 | 12 | class Commodity(models.Model): 13 | # 商品详情 14 | STATUS = ( 15 | (1, "在售"), 16 | (2, "缺货"), 17 | (3, "下架"), 18 | ) 19 | name = models.CharField(verbose_name='商品名称', max_length=255) 20 | price = models.FloatField(verbose_name="商品价格") 21 | discount = models.FloatField(verbose_name="商品折扣") 22 | status = models.IntegerField(verbose_name="商品状态", choices=STATUS) 23 | ptime = models.DateTimeField("上架时间", auto_now=True) 24 | context = RichTextUploadingField(null=True) 25 | img = models.ImageField(verbose_name="商品图片", upload_to='imgs/') 26 | 27 | class Meta: 28 | verbose_name = verbose_name_plural = "商品信息" 29 | -------------------------------------------------------------------------------- /socar/shop/static/media/imgs/640.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/static/media/imgs/640.jpg -------------------------------------------------------------------------------- /socar/shop/static/media/imgs/下载.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/static/media/imgs/下载.jpeg -------------------------------------------------------------------------------- /socar/shop/static/media/upload/2018/05/22/8c1001e93901213fab66256a58e736d12f2e952a.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/static/media/upload/2018/05/22/8c1001e93901213fab66256a58e736d12f2e952a.jpg -------------------------------------------------------------------------------- /socar/shop/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 在线商城 8 | 9 | 10 | 11 | {% include "nav.html" %} 12 | 13 | {% block body %} 14 |

页面主体

15 | {% endblock%} 16 | 17 | {% include "foot.html" %} 18 | 19 | -------------------------------------------------------------------------------- /socar/shop/templates/buy.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block body %} 4 | 5 | 6 |
7 | 总价格—:{{ price }} 8 |
9 | 10 | {% endblock %} -------------------------------------------------------------------------------- /socar/shop/templates/carinfo.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block body %} 4 | 5 | 6 |
7 | {% for k, v in goods.items %} 8 |
  • 商品名称:{{ k }}- 商品数量:{{ v }} 件
  • 9 | {% endfor %} 10 |
    11 |
    12 | 立即购买 13 |
    14 | 15 | {% endblock %} -------------------------------------------------------------------------------- /socar/shop/templates/create.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block body %} 4 | 5 | 6 |
    7 |
    8 | {% csrf_token %} 9 | {{ form.as_p }} 10 | 11 |
    12 |
    13 | 14 | {% endblock %} -------------------------------------------------------------------------------- /socar/shop/templates/foot.html: -------------------------------------------------------------------------------- 1 |
    2 | 5 |
    -------------------------------------------------------------------------------- /socar/shop/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block body %} 3 |

    4 | {% load upper %} 5 | {% upper %} 6 | 欢迎来到aigo商城 7 | {% endupper %} 8 |

    9 | 24 |
    25 | 过滤器{{ hello | ass}} 26 |
    27 | {% endblock %} 28 | 29 | -------------------------------------------------------------------------------- /socar/shop/templates/info.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block body %} 4 | 5 |
    6 |

    商品详情

    7 |
    8 | 9 | {% for key,value in good.items %} 10 | {% if key != "_state" %} 11 | {% if key == "context" %} 12 | 13 | {{key}} 14 |
    {{ value | safe }}
    15 | 16 | {% else %} 17 | 18 | 19 | {{key}} 20 |
    {{ value }}
    21 | 22 | {% endif %} 23 | 24 | {% endif %} 25 | {% endfor %} 26 | {% with tid=good.id %} 27 | 28 | {% csrf_token %} 29 | {{ form.as_table }} 30 | 31 | 32 | 33 | {% endwith %} 34 |
    35 | 36 | {% endblock %} -------------------------------------------------------------------------------- /socar/shop/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block body %} 4 | 5 | {% if form.errors %} 6 |

    Your username and password didn't match. Please try again.

    7 | {% endif %} 8 | 9 | {% if next %} 10 | {% if user.is_authenticated %} 11 |

    Your account doesn't have access to this page. To proceed, 12 | please login with an account that has access.

    13 | {% else %} 14 |

    Please login to see this page.

    15 | {% endif %} 16 | {% endif %} 17 | 18 |
    19 | {% csrf_token %} 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
    {{ form.username.label_tag }}{{ form.username }}
    {{ form.password.label_tag }}{{ form.password }}
    30 | 31 | 32 | 33 |
    34 | 35 | {# Assumes you setup the password_reset view in your URLconf #} 36 |

    Lost password?

    37 | 38 | {% endblock %} -------------------------------------------------------------------------------- /socar/shop/templates/nav.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /socar/shop/templates/perinfo.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | -------------------------------------------------------------------------------- /socar/shop/templates/tmp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 |
    11 | {% csrf_token %} 12 | {{ form.as_p }} 13 | 14 |
    15 | 16 | -------------------------------------------------------------------------------- /socar/shop/templatetags/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/shop/templatetags/__init__.py -------------------------------------------------------------------------------- /socar/shop/templatetags/upper.py: -------------------------------------------------------------------------------- 1 | from django import template 2 | 3 | 4 | register = template.Library() 5 | 6 | 7 | @register.tag 8 | def upper(parser, token): 9 | nodelist = parser.parse("endupper") 10 | parser.delete_first_token() 11 | return UpperNode(nodelist) 12 | 13 | 14 | class UpperNode(template.Node): 15 | def __init__(self, nodelist): 16 | self.nodelist = nodelist 17 | 18 | def render(self, context): 19 | content = self.nodelist.render(context) 20 | return content.upper() 21 | 22 | 23 | # 自定义过滤器 24 | def ass(value1): 25 | return value1 + "world" 26 | 27 | 28 | register.filter("ass", ass) 29 | -------------------------------------------------------------------------------- /socar/shop/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /socar/shop/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | from .views import index, create, info, a, Index, carpage, buy 3 | from django.contrib.auth.views import logout, LoginView 4 | 5 | urlpatterns = [ 6 | path('', index, name='index'), 7 | path("create/", create, name="create"), 8 | path("accounts/login/", LoginView.as_view(template_name='login.html'), name="login"), 9 | # 上面这个函数需要设置next 在html中设置 10 | path("accounts/", include('django.contrib.auth.urls')), 11 | path('ckeditor/', include('ckeditor_uploader.urls')), 12 | path("/", info, name="info"), 13 | path("logout/", logout, name="logout"), 14 | path("a/", a), 15 | path("carinfo/", carpage, name="carinfo"), 16 | path("buy", buy, name="buy"), 17 | 18 | ] -------------------------------------------------------------------------------- /socar/shop/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from django.http import Http404, HttpResponse 3 | from .models import Commodity, Nuser 4 | from .forms import BuyForm 5 | from django.contrib.auth.hashers import make_password, check_password 6 | from django.contrib.auth import authenticate, login, logout 7 | from django.contrib.auth.forms import UserCreationForm 8 | from django.contrib.auth.decorators import login_required 9 | from .forms import MyForm 10 | from django.views.generic import ListView 11 | # Create your views here. 12 | 13 | 14 | class Index(ListView): 15 | template_name = "index.html" 16 | model = Commodity 17 | paginate_by = 100 18 | # def get(self, request, *args, **kwargs): 19 | # goods = Commodity.objects.all() 20 | # return render(request, 'index.html', context={ 21 | # 'goods': goods, 22 | # 'hello': "hello", 23 | # }) 24 | 25 | 26 | def index(request): 27 | goods = Commodity.objects.all() 28 | return render(request, 'index.html', context={ 29 | 'goods': goods, 30 | 'hello': "hello", 31 | }) 32 | 33 | 34 | class CreationForm(UserCreationForm): 35 | 36 | class Meta(UserCreationForm.Meta): 37 | model = Nuser 38 | 39 | 40 | def create(request): 41 | 42 | if request.method == "POST": 43 | user = CreationForm(request.POST) 44 | if user.is_valid(): 45 | # username = request.POST.get("username") 46 | # passwd1 = request.POST.get("passwd1") 47 | # passwd2 = request.POST.get("passwd2") 48 | # nick = request.POST.get("nick") 49 | print(user.cleaned_data.items()) 50 | print(user.cleaned_data.keys()) 51 | user.save() 52 | # if passwd1 == passwd2: 53 | # MyUser.objects.create(username=username, passwd1=make_password(passwd1), passwd2=make_password(passwd2), nick=nick) 54 | return HttpResponse(content="注册成功") 55 | 56 | return render(request, "create.html", context={ 57 | "form": CreationForm()}) 58 | 59 | 60 | def user_logout(request): 61 | print(request.user) 62 | if request.user.is_authenticated: 63 | print(request.sessions) 64 | logout(request) 65 | return HttpResponse("退出成功") 66 | else: 67 | return HttpResponse("您还没有登陆过") 68 | 69 | 70 | def info(request, id): 71 | good = Commodity.objects.filter(id=id)[0] 72 | 73 | if request.method == "GET": 74 | try: 75 | return render(request, 'info.html', context={ 76 | 'good': good.__dict__, 77 | 'form': BuyForm(), 78 | }) 79 | 80 | except Exception as e: 81 | raise Http404(e) 82 | elif request.method == "POST": 83 | form = BuyForm(request.POST) 84 | if form.is_valid(): 85 | print(form.cleaned_data) 86 | response = HttpResponse(str(good.price * form.cleaned_data.get("num"))) 87 | request.user.car.update({ 88 | good.id: form.cleaned_data.get("num") 89 | }) 90 | return response 91 | else: 92 | 93 | return render(request, 'info.html', context={ 94 | 'good': good.__dict__, 95 | 'form': form, 96 | }) 97 | 98 | 99 | def perinfo(request, nick): 100 | return render(request, "perinfo.html") 101 | 102 | 103 | def a(request): 104 | if request.method == "GET": 105 | 106 | return render(request, "tmp.html", context={ 107 | "form": MyForm()}) 108 | 109 | if request.method == "POST": 110 | form = MyForm(request.POST) 111 | if form.is_valid(): 112 | print(form.cleaned_data) 113 | return HttpResponse("success") 114 | else: 115 | return render(request, "tmp.html", context={ 116 | "form": form}) 117 | 118 | 119 | 120 | # 购物车页面 121 | def carpage(request): 122 | return render(request, template_name='carinfo.html', context={ 123 | 'goods': request.user.car 124 | }) 125 | 126 | @login_required 127 | def buy(request): 128 | # 购买页面 129 | price = 0 130 | for k, v in request.user.car.items(): 131 | good = Commodity.objects.get(id=k) 132 | price += good.price * v 133 | 134 | return render(request, template_name='buy.html', context={ 135 | 'price': price 136 | }) 137 | 138 | 139 | -------------------------------------------------------------------------------- /socar/socar/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/socar/socar/__init__.py -------------------------------------------------------------------------------- /socar/socar/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for socar 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 = '%3dhyhpb--gq_pp_@!+*pl4b5wt1w5*-g(+45_9jqt#6$!paa@' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | LOGIN_URL = '/accounts/login/' 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 | 'shop', 42 | 'ckeditor', 43 | 'ckeditor_uploader', 44 | 'restfulshop', 45 | 'rest_framework', 46 | 47 | ] 48 | 49 | REST_FRAMEWORK = { 50 | # Use Django's standard `django.contrib.auth` permissions, 51 | # or allow read-only access for unauthenticated users. 52 | 'DEFAULT_PERMISSION_CLASSES': [ 53 | 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' 54 | ] 55 | } 56 | 57 | MIDDLEWARE = [ 58 | 'django.middleware.cache.UpdateCacheMiddleware', 59 | 'django.middleware.security.SecurityMiddleware', 60 | 'django.contrib.sessions.middleware.SessionMiddleware', 61 | 'django.middleware.common.CommonMiddleware', 62 | 'django.middleware.csrf.CsrfViewMiddleware', 63 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 64 | 'django.contrib.messages.middleware.MessageMiddleware', 65 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 66 | 'shop.middles.ShopCar', 67 | 'django.middleware.cache.FetchFromCacheMiddleware' 68 | ] 69 | 70 | ROOT_URLCONF = 'socar.urls' 71 | 72 | TEMPLATES = [ 73 | { 74 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 75 | 'DIRS': [os.path.join(BASE_DIR, './shop/templates')], 76 | 'APP_DIRS': True, 77 | 'OPTIONS': { 78 | 'context_processors': [ 79 | 'django.template.context_processors.debug', 80 | 'django.template.context_processors.request', 81 | 'django.contrib.auth.context_processors.auth', 82 | 'django.contrib.messages.context_processors.messages', 83 | 'django.template.context_processors.media', 84 | ], 85 | }, 86 | }, 87 | ] 88 | 89 | WSGI_APPLICATION = 'socar.wsgi.application' 90 | 91 | 92 | # Database 93 | # https://docs.djangoproject.com/en/2.0/ref/settings/#databases 94 | 95 | DATABASES = { 96 | 'default': { 97 | 'ENGINE': 'django.db.backends.sqlite3', 98 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 99 | } 100 | } 101 | 102 | 103 | # Password validation 104 | # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators 105 | 106 | AUTH_PASSWORD_VALIDATORS = [ 107 | { 108 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 109 | }, 110 | { 111 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 112 | }, 113 | { 114 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 115 | }, 116 | { 117 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 118 | }, 119 | ] 120 | 121 | 122 | # 缓存 123 | CACHES = { 124 | 'default': { 125 | 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 126 | 'LOCATION': 'unique-snowflake' 127 | } 128 | } 129 | 130 | 131 | # Internationalization 132 | # https://docs.djangoproject.com/en/2.0/topics/i18n/ 133 | 134 | LANGUAGE_CODE = 'zh-hans' 135 | 136 | TIME_ZONE = 'Asia/Shanghai' 137 | 138 | USE_I18N = True 139 | 140 | USE_L10N = True 141 | 142 | USE_TZ = True 143 | 144 | AUTH_USER_MODEL = "shop.Nuser" 145 | 146 | # Static files (CSS, JavaScript, Images) 147 | # https://docs.djangoproject.com/en/2.0/howto/static-files/ 148 | 149 | STATIC_URL = '/static/' 150 | STATICFILES_DIRS = [ 151 | os.path.join(BASE_DIR, 'shop/static') 152 | ] 153 | 154 | MEDIA_URL = '/static/media/' 155 | MEDIA_ROOT = os.path.join(BASE_DIR, 'shop/static/media/') 156 | CKEDITOR_UPLOAD_PATH = 'upload/' 157 | 158 | 159 | # Django 只在需要的时候才送出cookie。 如果你压根儿就没有设置任何会话数据,它不会 送出会话cookie(除非 SESSION_SAVE_EVERY_REQUEST 设置为 True )。 160 | SESSION_SAVE_EVERY_REQUEST = True 161 | -------------------------------------------------------------------------------- /socar/socar/urls.py: -------------------------------------------------------------------------------- 1 | """socar 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, include, re_path 18 | from shop import urls as surls 19 | from restfulshop import urls 20 | 21 | urlpatterns = [ 22 | path('admin/', admin.site.urls), 23 | path('', include(surls)), 24 | path("api/", include(urls)) 25 | ] 26 | -------------------------------------------------------------------------------- /socar/socar/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for socar 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", "socar.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxduck/Shop/49b61ec27b1629226ed8fbfe1d8e2eb61cf82d85/tests/.gitkeep --------------------------------------------------------------------------------