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
完整程序源代碼下載地址:
https://download.csdn.net/download/weixin_42756970/86819049
程序運行截圖
后臺管理
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),
]
完整程序源代碼下載地址:
https://download.csdn.net/download/weixin_42756970/86819049
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
源代碼
+關注
關注
96文章
2945瀏覽量
66747 -
MySQL
+關注
關注
1文章
809瀏覽量
26565 -
python
+關注
關注
56文章
4797瀏覽量
84683
發布評論請先 登錄
相關推薦
評論