***************************************************************************************************
*********** * 一.项目框架搭建*************
***************************************************************************************************
1.安装虚拟环境,pip install virtualenv pip install virtualenvwrapper-win
查看版本号:virtualenv -version
2.创建python3版本的虚拟环境 mkvirtualenv -p "E:\install\python\python3\python3.exe" pyp
在ubuntu中创建不用这么麻烦,直接 mkvirtualenv -p python3 pyp
查看该环境下安装了哪些包:pip list pip freeze
3.进入虚拟环境:先workon查看有哪些虚拟环境,进入虚拟环境workon pyp
4.安装Django:pip install Django==1.8.2
5.创建项目:django-admin startproject test1
cd test1
6.创建运用:python manage.py startapp book
7.打开pycharm,指定项目的运行环境,在c盘下的envs/环境名/scripts/python.exe,然后将运用注册到setting中去
================================================
要安装pip install pymysql
pip install Django-tinymce==2.6.0
pip install Pillow
一.项目多的时候,要创建一个apps来进行统一管理:
注册这些应用有两种写法
1.在settings的INSTALLED_APPS中注册这些应用'apps.user'等。
2.让项目可以自动识别:
(1)settings中加入import sys
sys.path.insert(0,os.path.join(BASE_DIR,"apps"))#让项目可以识别app目录,sys.path用于动态的去修改路径
(2)点击apps,打开Mark Directory as点击Sources Root,然后在settings的INSTALLED_APPS中注册这些应用'user'等
二.配置模板目录:在settings的TEMPLATES中写'DIRS': [os.path.join(BASE_DIR,"templates")],
================================================================
mysql数据库的配置,在setting里面
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "dailyfresh1807",
'USER': "root",
'PASSWORD': "root",
'HOST': "127.0.0.1",
'PORT': "3306",
}
}
============================================
后台的相关配置,让后台语言为中文简体
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
============================================
配置静态文件目录
STATIC_URL = '/static/' #配置项目中静态文件访问的路径
STATICFILES_DIRS = [os.path.join(BASE_DIR,"static")] #配置项目文件中的静态文件的存储位置
============================================
项目中引入应用的urls.py文件,尽量的使用反向解析,帮助我们动态的生成路径,此为一级路由
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^tinymce/', include("tinymce.urls")), #富文本编辑器
url(r'^user/', include("user.urls",namespace="user")), #用户模块
url(r'^cart/', include("cart.urls",namespace="cart")), #购物车模块
url(r'^order/', include("order.urls",namespace="order")), #订单模块
url(r'', include("goods.urls",namespace="goods")), #商品模块
]
在创建的各个运用里都加入urls.py文件
=======================================================
富文本插件
1.注册:settings的INSTALLED_APPS中写入tinymce,
2.在setting配置:
TINYMCE_DEFAULT_CONFIG = {
'theme':'advanced',
'width': 600,
'height': 400
}
3.在一级的urls的admin配置的路由下写入
from django.conf.urls import include, url
from user import views
from user.views import RegisterView,ActiveView,LoginView
url(r'^tinymce/', include("tinymce.urls")), #富文本编辑器
***************************************************************************************************
************************ *二.创建模型类* ********************
***************************************************************************************************
SPU商品信息集合的最小单位,属性特性相同的商品就一个SPU
SKU库存进出计量的最小单位,件/盒/托盘等
1.基本模型类
在总项目下新建db/base_model.py
from django.db import models
class BaseModel(models.Model):
create_time = models.DateField(auto_now_add=True,verbose_name="创建时间")
update_time = models.DateField(auto_now_add=True,verbose_name="更新时间")
is_delete= models.BooleanField(default=False,verbose_name="删除标记")
class Meta:
#抽象的模型类,最后不会生成表
abstract = True
==================================================
2.用户模型类
在user/models.py中
from django.db import models
from django.contrib.auth.models import AbstractUser
from db.base_model import BaseModel
# Create your models here.
class User(AbstractUser,BaseModel):
class Meta:
db_table = "df_user"
verbose_name = "用户"
verbose_name_plural = verbose_name
class Address(BaseModel):
user = models.ForeignKey("User",verbose_name="所属账户")
receiver = models.CharField(max_length=20,verbose_name="收件人")
addr = models.CharField(max_length=256,verbose_name="收件地址")
zip_code = models.CharField(max_length=6,null=True,verbose_name="邮政编码")
phone = models.CharField(max_length=11,verbose_name="联系电话")
is_defult = models.BooleanField(default=False,verbose_name="是否默认")
class Meta:
db_table = 'df_address'
verbose_name ="地址"
verbose_name_plural = verbose_name
====================================================
3.商品模块的模型类
from django.db import models
from db.base_model import BaseModel
from tinymce.models import HTMLField
# Create your models here.
class GoodsType(BaseModel):
name = models.CharField(max_length=20, verbose_name="种类名称")
logo = models.CharField(max_length=20, verbose_name="标识")
image = models.ImageField(upload_to="type", verbose_name="商品类型图片")
cover_image = models.ImageField(upload_to="type",verbose_name="商品类型封面图")
class Meta:
db_table = 'df_goods_type'
verbose_name = '商品种类'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Goods(BaseModel):
name = models.CharField(max_length=20,verbose_name="商品spu名称")
detail = HTMLField(blank=True,verbose_name="商品详情")
def __str__(self):
return self.name
class Meta:
db_table = "df_goods"
verbose_name = "商品SPU"
verbose_name_plural = verbose_name
class GoodsImage(BaseModel):
sku = models.ForeignKey("GoodsSKU",verbose_name="商品")
image = models.ImageField(upload_to="goods",verbose_name="图片路径")
class Meta:
db_table = "df_goods_image"
verbose_name = "商品图片"
verbose_name_plural = verbose_name
class GoodsSKU(BaseModel):
status_choices = (
(0,"上线"),
(1,"下线"),
)
type = models.ForeignKey("GoodsType",verbose_name="商品种类")
goods = models.ForeignKey("Goods",verbose_name="商品SPU")
name = models.CharField(max_length=20,verbose_name="商品名称")
desc = models.CharField(max_length=256,verbose_name="商品简介")
price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="商品价格")
unite = models.CharField(max_length=20,verbose_name="商品单位")
image = models.ImageField(upload_to="goods",verbose_name="商品图片")
stock = models.IntegerField(default=1,verbose_name="商品库存")
sales = models.IntegerField(default=0,verbose_name="商品销量")
status = models.SmallIntegerField(default=1,choices=status_choices,verbose_name="商品状态")
def __str__(self):
return self.name
class Meta:
db_table = "df_goods_sku"
verbose_name ="商品"
verbose_name_plural = verbose_name
class IndexGoodsBanner(BaseModel):
sku = models.ForeignKey("GoodsSKU",verbose_name="商品")
image = models.ImageField(upload_to="banner",verbose_name="图片")
index = models.SmallIntegerField(default=0,verbose_name="展示顺序")
def __str__(self):
return self.sku.name
class Meta:
db_table = "df_index_banner"
verbose_name="首页轮播图片"
verbose_name_plural = verbose_name
class IndexTypeGoodBanner(BaseModel):
DISPLAY_TYPE_CHOICES = (
(0,"标题"),
(1,"图片"),
)
type = models.ForeignKey("GoodsType",verbose_name="商品类型")
sku = models.ForeignKey("GoodsSKU",verbose_name="商品SKU")
display_type = models.SmallIntegerField(default=1,choices=DISPLAY_TYPE_CHOICES,verbose_name="商品显示类型")
index = models.SmallIntegerField(default=0,verbose_name="商品顺序")
def __str__(self):
return self.type.name +"===" +self.sku.name
class Meta:
db_table="df_index_type_goods"
verbose_name = "主页分类展示商品"
verbose_name_plural = verbose_name
class IndexPromotionBanner(BaseModel):
# 主页促销活动
name = models.CharField(max_length=20,verbose_name="活动名称")
# url = models.URLField(verbose_name="活动链接")
url = models.CharField(max_length=256,verbose_name="活动链接")
image = models.ImageField(upload_to="banner",verbose_name="活动图片")
index = models.SmallIntegerField(default=0,verbose_name="展示顺序")
def __str__(self):
return self.name
class Meta:
db_table = "df_index_promotion"
verbose_name = "主页促销活动"
verbose_name_plural = verbose_name
========================================================
4.订单模型类:
from django.db import models
from db.base_model import BaseModel
# Create your models here.
class OrderInfo(BaseModel):
PAY_METHOD_CHOICES = (
(1,"货到付款"),
(2,"微信支付"),
(3,"支付宝"),
(4,"银联支付")
)
ORDER_STATUS_CHOICES = (
(1, "待支付"),
(2, "待发货"),
(3, "待收获"),
(4, "待评价"),
(5, "待完成")
)
order_id = models.CharField(max_length=126,primary_key=True,verbose_name="订单ID")
user = models.ForeignKey("user.User",verbose_name="用户")
addr = models.ForeignKey("user.Address",verbose_name="地址")
pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES,default=3,verbose_name="支付方式")
total_count =models.IntegerField(default=1,verbose_name="商品数量")
total_price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="商品总价")
transit_price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="订单运费")
order_status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES,default=1,verbose_name="订单状态")
trade_no = models.CharField(max_length=128,verbose_name="支付编号")
class Meta:
db_table = 'df_order_info'
verbose_name='订单'
verbose_name_plural = verbose_name
class OrderGoods(BaseModel):
order = models.ForeignKey("OrderInfo", verbose_name="订单")
sku = models.ForeignKey("goods.GoodsSKU", verbose_name="商品SKU")
count = models.IntegerField(default=1, verbose_name="商品数目")
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品价格")
comment = models.CharField(max_length=256,default='',verbose_name='评论')
class Meta:
db_table = 'df_order_goods'
verbose_name='订单商品'
verbose_name_plural = verbose_name
========================================================
5.替换Django默认的认证系统
在setting中加入
#django认证的系统使用的模型类
AUTH_USER_MODEL = "user.User"
==========================================================
6.在总项目的__init__文件下写入
import pymysql
pymysql.install_as_MySQLdb()
==========================================================
7.进行生成迁移文件和执行迁移文件(前提是在数据库中已经将dailyfresh1807数据库建立好了 create databases dailyfresh1807 charset = "utf8";)
python manage.py makemigration
python manage.py migrate