程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python online mall source code, based on django+mysql+redis, supports Alipay payment

編輯:Python

Python Online mall source code , be based on Django+MySQL+Redis, Support Alipay payment , Realization : User login registration , Merchandise display , Product details screen , Search for products , Add items of different sizes, colors and quantities to the shopping cart , Shopping cart management , Address management , Forming an order , Alipay pay .
change netshop in settings Of DATABASES.
To start the payment function , Remove the order in urls.py Two of path Note before , as well as views.py Lidi 54 Line and comments on the last line , Fill in the public key of Alipay 、 Apply private key and appid.
Screenshot of program operation



Core program code
setting.py

""" Django settings for netshop project. Generated by 'django-admin startproject' using Django 4.0.3. For more information on this file, see https://docs.djangoproject.com/en/4.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-$kjc-+6u73ma^=xp7pf7)hspeb_9=++h+nb8hl&&uhjul%us!5'
# 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',
# my app
'goods',
'userapp',
'cart',
'order',
'utils',
]
MIDDLEWARE = [ # 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 = 'netshop.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',
'userapp.mycontextprocessors.getUserInfo', # Both modules and applications can share 
],
},
},
]
WSGI_APPLICATION = 'netshop.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',
'NAME': 'netshop',
'HOST': '127.0.0.1',
'POST': '3306',
'USER': 'root',
'PASSWORD': ''
}
}
# Password validation
# https://docs.djangoproject.com/en/4.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/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True # See the video 
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static\css'),
os.path.join(BASE_DIR,'static\js'),
os.path.join(BASE_DIR,'static\images'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# global_settings
CACHES = {

'default': {

'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
'session-redis': {

"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
}
}
SESSION_CACHE_ALIAS = 'session-redis'
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

goods/views.py

from django.shortcuts import render
# Create your views here.
from django.views import View
from goods.models import *
from django.core.paginator import Paginator
import math
class IndexView(View):
def get(self, request, cid=2, num=1, *args, **kwargs):
cid = int(cid)
num = int(num)
# Query all category information 
categorys = Category.objects.all().order_by('id')
# Query all product information under the current category 
goodsList = Goods.objects.filter(category_id=cid).order_by('id')
# Pagination Each page displays eight records 
pager = Paginator(goodsList, 8)
# Gets the data of the current page 
page_goodsList = pager.page(num)
# Page number at the beginning of each page 
begin = (num - int(math.ceil(10.0 / 2)))
if begin < 1:
begin = 1
# End of each page page 
end = begin + 9
if end > pager.num_pages:
end = pager.num_pages
if end <= 10:
begin = 1
else:
begin = end - 9
pagelist = range(begin, end + 1)
return render(request, 'index.html',
{
'categorys': categorys, 'goodsList': page_goodsList, 'currentcid': cid, 'pagelist': pagelist,
'currentNum': num})
def recommed_view(func): # How to put it? Because now only the data of women's clothing The others didn't If you simply browse women's clothes This method is a historical record But if you browse other categories, you won't recommend women's clothes 
def wrapper(detailView, request, goodsid, *args, **kwargs):
# Will be stored in cookie Medium goodsid obtain 
cookie_str = request.COOKIES.get('recommend', '')
# Deposit goodsID Put it in there 
goodsidlist = [gid for gid in cookie_str.split() if gid.strip()]
# Finally, you need to get the recommended products 
goodsobjlist = [Goods.objects.get(id=gsid) for gsid in goodsidlist if
gsid != goodsid and Goods.objects.get(id=gsid).category_id == Goods.objects.get(id=goodsid).category_id][:4]
# id Objects are placed inside id Corresponding to the attributes of the whole product The query results in the whole goodislist All objects of , But only four are shown below , Just slice the list 
# take goodsliest Pass to get Method 
response = func(detailView, request, goodsid, goodsobjlist, *args, **kwargs)
# This place can be understood as calling get Method , Then it returns to the browser with a response Object , It's just that this place is here , Very basic general operation , Pay attention to the 
# Judge goodsid Whether there is goodsidlist in 
if goodsid in goodsidlist:
goodsidlist.remove(goodsid)
goodsidlist.insert(0, goodsid)
else:
goodsidlist.insert(0, goodsid)
# take goosidlist The data in is saved to cookid in 
response.set_cookie('recommend', ' '.join('%s' % gsid for gsid in goodsidlist), max_age=3 * 24 * 60 * 60)
return response
return wrapper
class DetailView(View):
@recommed_view
def get(self, request, goodsid, recommendlist=[]):
goodsid = int(goodsid)
# according to goodsid Search for product details ,(goods object )
goods = Goods.objects.get(id=goodsid)
category = Category.objects.get(id=goods.category_id).cname
return render(request, 'detail.html', {
'goods': goods, 'recommendlist': recommendlist, 'category': category})
class SearchView(View):
def get(self, request, num=1, *args, **kwargs):
num = int(num)
search = request.GET.get('search', '')
goods = Goods.objects.filter(gname__contains=search)
return render(request, 'search.html', {
'goods': goods})

Complete source code download address :Python Online mall source code
more Python Source code :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved