上次我们已经对网站业务等做出分析,也设计好表的结构了,接下来创建项目:
首先,建立项目需要的虚拟环境(创建一个包,最好创建一个,单个项目独立拥有包,在客户安装时会更方便):
virtualenv shopingwebenv
source shopingwebenv/bin/activate
建立项目和app(这里app三个,用户,购物车,商品)userinfo,cartinfo,memberapp:
django-admin startproject shopingweb
python3 manage.py startapp userinfo
python3 manage.py startapp cartinfo
python3 manage.py startapp memberapp
配置settings和urls就不详细讲了,可以留言或者查其他资料,接下来我们开始最开心的部分,码代码==。
这里我用的pycharm打开项目,首先写userinfo,表的字段类型我就直接写在代码里了,设计表:
from django.db import models
# Create your models here.
class UserInfo(models.Model):
uname = models.CharField('用户名',max_length=50,null=False)
upassword = models.CharField('密码',max_length=200,null=False)
email = models.CharField('邮箱',max_length=50,null=True)
phone = models.CharField('手机号',max_length=20,null=False)
time = models.DateTimeField('注册时间',auto_now=True)
isban = models.BooleanField('是否禁用',default=False)
isdelete = models.BooleanField('删除',default=False)
def __str__(self):
return self.uname
class Adress(models.Model):
aname = models.CharField('收货人',max_length=50,null=False)
ads = models.CharField('地址',max_length=300,null=False)
phone = models.CharField('电话',max_length=20,null=False)
user = models.ForeignKey(UserInfo,on_delete=models.CASCADE)
def __str__(self):
return self.aname
cartinfo:
from django.db import models
from userinfo.models import UserInfo
from memberapp.models import Goods
# Create your models here.
# 购物车CartInfo
# id
# user 用户(关联UserInfo)
# goods 商品(关联Goods)
# ccount 数量(数量)
ORDERSTATUS = (
(1,'未支付',),
(2,'已支付',),
(3,'订单取消',),
)
class CartInfo(models.Model):
user = models.ForeignKey(UserInfo,db_column='user_id')
good = models.ForeignKey(Goods,db_column='good_id')
ccount = models.IntegerField('数量',db_column='cart_count')
def __str__(self):
return self.user
# 订单表Order
# id
# orderNo 订单号
# orderdetail(商品,数量,单价,描述)
# adsname 收件人
# adsphone 收件电话
# ads 地址
# user 用户(关联)
# time 时间
# acot 总数
# acount 总价
# orderstatus 状态
class Order(models.Model):
orderNo = models.CharField('商品编号',max_length=50)
orderdetail = models.TextField('订单详情')
adsname = models.CharField('收件人姓名',max_length=30,null=False)
adsphone = models.CharField('收件人电话',max_length=20,null=False)
ads = models.CharField('地址',max_length=300)
time = models.DateTimeField(auto_now=True)
acot = models.IntegerField('总数')
acount = models.DecimalField('总价',max_digits=8,decimal_places=2)
orderstatus = models.IntegerField('订单',choices=ORDERSTATUS,default=1)
user = models.ForeignKey(UserInfo)
def __str__(self):
return self.orderNo
memberapp:
from django.db import models
# Create your models here.
class GoodsType(models.Model):
title = models.CharField('分类名称',max_length=30)
desc = models.CharField('描述',max_length=200,default='商品描述')
isdelete = models.BooleanField('删除',default=False)
def __str__(self):
return self.title
class Goods(models.Model):
title = models.CharField('商品名称',max_length=30,null=False)
price = models.DecimalField('商品价格',max_digits=8,decimal_places=2)
desc = models.CharField('描述', max_length=200)
unit = models.CharField('单位',max_length=30)
picture = models.ImageField('商品图片',upload_to='static/images/goods',default='normal.png')
detail = models.CharField('商品详情',max_length=1000,default='商品详情')
isdelete = models.BooleanField('删除',default=False)
type = models.ForeignKey(GoodsType,on_delete=models.CASCADE)
def __str__(self):
return self.title
先写到这,慢慢补后边的