Python版超市管理系统源代码,基于django+mysql
安装步骤
1、在mysql中创建名为demo_django_supermarket的数据库,修改config/setting.py中数据库用户及密码
CREATE DATABASE demo_django_supermarket DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2、pip install -r requirements
3、初始化数据库:manage.py migrate
4、设置一个超级管理员 admin (admin@123456)
manage.py loaddata fixtures/root_manager.yaml
5、启动服务
manage.py runserver localhost:8001
完整程序源代码下载地址:Python版超市管理系统源代码
程序运行截图
后台管理
setting.py
"""
Django settings for config project.
Generated by 'django-admin startproject' using Django 2.1.4.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/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.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_joao(!w!oc6ktbxr55x4$ioy4$#u6#09cx$st=pp3sj(6lm!)'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*',
# '127.0.0.1',
# '10.10.10.154',
# '120.229.216.9'
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'gunicorn',
]
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 = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, os.path.join('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 = 'config.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'demo_django_supermarket',
'USER': 'root',
'PASSWORD': 'sxing86',
'OPTIONS': {
'charset': 'utf8mb4'
}
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/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.1/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# USE_TZ = True # 时区
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_PATH = os.path.dirname(os.path.abspath(__file__))
STATIC_PATH = os.path.join(STATIC_PATH, '../')
STATICFILES_DIRS = (
os.path.join(STATIC_PATH, 'static/'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
0001_initial.py
# Generated by Django 2.2.2 on 2022-03-16 18:26
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Goods',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20)),
('sale_price', models.FloatField()),
('cost_price', models.FloatField()),
('weight', models.FloatField()),
('sort', models.IntegerField()),
('produce_date', models.DateField()),
('limit_date', models.DateField()),
('lower', models.FloatField(default=0)),
('isDelete', models.BooleanField(default=False)),
],
),
migrations.CreateModel(
name='Manager',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('account', models.CharField(max_length=20)),
('pwd', models.CharField(max_length=40)),
('name', models.CharField(max_length=20)),
('gender', models.IntegerField(default=0)),
('phone', models.CharField(max_length=11)),
('authority', models.IntegerField(default=0)),
('isDelete', models.BooleanField(default=0)),
],
),
migrations.CreateModel(
name='Message',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('time', models.DateTimeField(auto_now=True)),
('type', models.IntegerField()),
('content', models.TextField()),
('contact', models.CharField(max_length=20)),
('name', models.CharField(max_length=20)),
('isRead', models.BooleanField(default=False)),
],
),
migrations.CreateModel(
name='Provider',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20)),
('address', models.CharField(max_length=40)),
('phone', models.CharField(max_length=11)),
('isDelete', models.BooleanField(default=False)),
],
),
migrations.CreateModel(
name='Record',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('location', models.IntegerField()),
('date', models.DateField(auto_now=True)),
('purchase_num', models.IntegerField(null=True)),
('sale_num', models.IntegerField(null=True)),
('withdraw_num', models.IntegerField(null=True)),
('goods', models.ForeignKey(on_delete=True, to='app.Goods')),
],
),
migrations.AddField(
model_name='goods',
name='provider',
field=models.ForeignKey(on_delete=True, to='app.Provider'),
),
]
0002_initial_data.py
# Generated by Django 2.2.2 on 2022-03-16 18:26
import datetime
import os
import random
import shutil
from django.db import migrations
def init_manager(apps, args):
Manager = apps.get_model('app', 'Manager')
Manager(
account="admin",
pwd="123456",
name="admin",
gender=1,
phone="15512345678",
).save()
def init_providers(apps, args):
provider = apps.get_model('app', 'Provider')
for i in range(3):
p = provider()
p.name = "供应商%d" % i
p.address = "(%s)请关注公众号:Python代码大全" % p.name
p.phone = "15512345678%d" % i
p.save()
def init_goods(apps, args):
category_map = {
0: "零食饮料",
1: "生鲜果蔬",
2: "粮油副食",
3: "清洁用品",
4: "家居家电",
}
# 循环 /static/media/resources/goods/*
current_path = os.path.dirname(os.path.abspath(__file__))
current_path = current_path.replace("app\migrations", "")
resource_img_dir = "%s/static/media/resources/goods" % current_path
upload_img_dir = "%s/static/media/goods_img" % current_path
files = [f for f in os.listdir(resource_img_dir) if os.path.isfile(os.path.join(resource_img_dir, f))]
print(files)
# 复制+改名 成 /static/media/goods_img/{category_num}_{good_id}.jpg
date_now = datetime.datetime.utcnow()
good = apps.get_model('app', 'Goods')
record = apps.get_model('app', 'Record')
for index, f in enumerate(files):
category_id = random.randrange(0, 5)
provider_id = random.randrange(1, 4)
cost_price = random.randrange(5, 100)
sale_price = cost_price + random.randrange(5, 20)
produce_date = date_now - datetime.timedelta(days=(category_id + 1) * 365)
limit_date = date_now + datetime.timedelta(days=(category_id + 1) * 365)
purchase_date = produce_date + datetime.timedelta(days=cost_price)
sale_date = purchase_date + datetime.timedelta(days=sale_price)
# total = good.objects.count()
# 随机造数据
g = good(
name=category_map[category_id] + str(index),
sort=category_id,
cost_price=float(cost_price),
sale_price=float(sale_price),
produce_date=produce_date,
limit_date=limit_date,
weight=float(sale_price + cost_price),
provider_id=provider_id,
)
# g.id = total+1
g.save()
image_path = "%s/%d_%d.png" % (upload_img_dir, category_id, g.id)
shutil.copyfile("%s/%s" % (resource_img_dir, f), image_path)
location_id = random.randrange(0, 8)
record(
location=location_id,
purchase_num=sale_price * cost_price,
goods_id=g.id,
date=purchase_date,
).save()
record(
location=location_id,
goods_id=g.id,
date=sale_date,
sale_num=sale_price * cost_price / (location_id + 1),
).save()
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.RunPython(init_providers),
migrations.RunPython(init_goods),
migrations.RunPython(init_manager),
]
完整程序源代码下载地址:Python版超市管理系统源代码
本文含有隐藏内容,请 开通VIP 后查看