前提提要 登录注册完成
module表
用户表
class UserInfo(models.Model):
"""
用户表
"""
username = models.CharField(max_length=16)
password = models.CharField(max_length=32)
telephone = models.CharField(max_length=11)
email = models.EmailField()
is_active = models.BooleanField(default=True) #当前员工是否还是我的员工,False
depart = models.ForeignKey('Department', on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.username
部门表
class Department(models.Model):
"""
部门表
"""
name = models.CharField(max_length=32)
count = models.IntegerField()
def __str__(self):
return self.name
客户表
sex_type = (('male', '男性'), ('female', '女性')) #对应字段性别
source_type = (('qq', "qq群"),
('referral', "内部转介绍"),
('website', "官方网站"),
('baidu_ads', "百度推广"),
('office_direct', "直接上门"),
('WoM', "口碑"),
('public_class', "公开课"),
('website_luffy', "路飞官网"),
('others', "其它"),)
course_choices = (('LinuxL', 'Linux中高级'),
('PythonFullStack', 'Python高级全栈开发'),)
class_type_choices = (('fulltime', '脱产班',),
('online', '网络班'),
('weekend', '周末班',),)
class Customer(models.Model):
"""
客户表(最开始的时候大家都是客户,销售就不停的撩你,你还没交钱就是个客户)
"""
qq = models.CharField(verbose_name='QQ', max_length=64, unique=True, help_text='QQ号必须唯一')
qq_name = models.CharField(verbose_name='QQ昵称', max_length=64, blank=True, null=True) #requierd:False
name = models.CharField('姓名', max_length=32, blank=True, null=True, help_text='学员报名后,请改为真实姓名') #可为空,有些人就不愿意给自己的真实姓名
sex = models.CharField("性别", choices=sex_type, max_length=16, default='male', blank=True, null=True) #存的是male或者female,字符串
birthday = models.DateField('出生日期', default=None, help_text="格式yyyy-mm-dd", blank=True, null=True)
phone = models.BigIntegerField('手机号', blank=True, null=True) #手机号改成字符串的,不然不好搜索
source = models.CharField('客户来源', max_length=64, choices=source_type, default='qq')
introduce_from = models.ForeignKey('self', verbose_name="转介绍自学员", blank=True, null=True,on_delete=models.CASCADE) #self指的就是自己这个表,和
course = MultiSelectField("咨询课程", choices=course_choices)
class_type = models.CharField("班级类型", max_length=64, choices=class_type_choices, default='fulltime')
customer_note = models.TextField("客户备注", blank=True, null=True, )
status = models.CharField("状态", choices=enroll_status_choices, max_length=64, default="unregistered",help_text="选择客户此时的状态") #help_text这种参数基本都是针对admin应用里面用的
date = models.DateTimeField("咨询日期", auto_now_add=True) #这个没啥用昂,我问销售,销售说是为了一周年的时候给客户发一个祝福信息啥的
last_consult_date = models.DateField("最后跟进日期", auto_now_add=True) #考核销售的跟进情况,如果多天没有跟进,会影响销售的绩效等
next_date = models.DateField("预计再次跟进时间", blank=True, null=True) #销售自己大概记录一下自己下一次会什么时候跟进,也没啥用
#用户表中存放的是自己公司的所有员工。
consultant = models.ForeignKey('UserInfo', verbose_name="销售", related_name='customers', blank=True, null=True,on_delete=models.CASCADE )
# 一个客户可以报多个班,报个脱产班,再报个周末班等,所以是多对多。
class_list = models.ManyToManyField('ClassList', verbose_name="已报班级",blank=True,)
# 成单日期,统计成单日期的时候会用到
deal_date = models.DateField(null=True)
class Meta:
ordering = ['id',]#排序作用
verbose_name='客户信息表'
verbose_name_plural = '客户信息表'#admin 表名字
def __str__(self):
return self.name+":"+self.qq
总结字段真多
校区表
class Campuses(models.Model):
"""
校区表
"""
name = models.CharField(verbose_name='校区', max_length=64)
address = models.CharField(verbose_name='详细地址', max_length=512, blank=True, null=True)
def __str__(self):
return self.name
班级表
from django.db import models
from multiselectfield import MultiSelectField
from django.utils.safestring import mark_safe
# Create your models here.
enroll_status_choices = (('signed', "已报名"),
('unregistered', "未报名"),
('studying', '学习中'),
('paid_in_full', "学费已交齐"))
seek_status_choices = (('A', '近期无报名计划'), ('B', '1个月内报名'), ('C', '2周内报名'), ('D', '1周内报名'),
('E', '定金'), ('F', '到班'), ('G', '全款'), ('H', '无效'),)
pay_type_choices = (('deposit', "订金/报名费"),
('tuition', "学费"),
('transfer', "转班"),
('dropout', "退学"),
('refund', "退款"),)
attendance_choices = (('checked', "已签到"),
('vacate', "请假"),
('late', "迟到"),
('absence', "缺勤"),
('leave_early', "早退"),)
score_choices = ((100, 'A+'),
(90, 'A'),
(85, 'B+'),
(80, 'B'),
(70, 'B-'),
(60, 'C+'),
(50, 'C'),
(40, 'C-'),
(0, ' D'),
(-1, 'N/A'),
(-100, 'COPY'),
(-1000, 'FAIL'),)
class ClassList(models.Model):
"""
班级表
"""
course = models.CharField("课程名称", max_length=64, choices=course_choices)
semester = models.IntegerField("学期") #python20期等
campuses = models.ForeignKey('Campuses', verbose_name="校区",on_delete=models.CASCADE)
price = models.IntegerField("学费", default=10000)
memo = models.CharField('说明', blank=True, null=True, max_length=100)
start_date = models.DateField("开班日期")
graduate_date = models.DateField("结业日期", blank=True, null=True) #不一定什么时候结业,哈哈,所以可为空
#contract = models.ForeignKey('ContractTemplate', verbose_name="选择合同模版", blank=True, null=True,on_delete=models.CASCADE)
teachers = models.ManyToManyField('UserInfo', verbose_name="老师") #对了,还有一点,如果你用的django2版本的,那么外键字段都需要自行写上on_delete=models.CASCADE
class_type = models.CharField(choices=class_type_choices, max_length=64, verbose_name='班级类型', blank=True,null=True)
class Meta:
unique_together = ("course", "semester", 'campuses')
def __str__(self):
return "{}{}({})".format(self.get_course_display(), self.semester, self.campuses)
用户信息展示
view
def customers(request):
all_customers = models.Customer.objects.all()
return render(request,'customer/customers.html',{'all_customers':all_customers})
html
{% extends 'starter.html' %}#模板继承
{% block title %}
<h1>
客户信息展示
</h1>
{% endblock %}
{% block content %}
<div class="container-fluid">
<a href="{% url 'addcustomer' %}" class="btn btn-primary">添加客户</a>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>序号</th>
<th>qq号</th>
<th>姓名</th>
<th>性别</th>
<th>咨询课程</th>
<th>状态</th>
<th>销售</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for customer in all_customers %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ customer.qq }}</td>
<td>{{ customer.name }}</td>
<td>{{ customer.sex }}</td>
<td>{{ customer.course }}</td>
<td>{{ customer.status }}</td>
<td>{{ customer.consultant }}</td>
<td>
<a href="{% url 'editcustomer' customer.pk %}" > <i class="fa fa-edit fa-2x"></i> </a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
用户信息删除
用户信息增加或编辑
增加页面的创造由modulform来形成
myforms
from sales import models
from django import forms
class CustomerModelForm(forms.ModelForm):
class Meta:
model = models.Customer
fields = '__all__'
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
for field_name,field in self.fields.items():
print(type(field))
from multiselectfield.forms.fields import MultiSelectFormField
if not isinstance(field,MultiSelectFormField):
field.widget.attrs.update({'class':'form-control'})
view
def addcustomer(request):
"""
添加
"""
if request.method == 'GET':
book_form_obj = myforms.CustomerModelForm()
return render(request,'customer/addcustomer.html',{'book_form_obj':book_form_obj})
else:
book_form_obj = myforms.CustomerModelForm(request.POST)
if book_form_obj.is_valid():
book_form_obj.save()
return redirect('customers')
else:
return render(request, 'customer/addcustomer.html', {'book_form_obj': book_form_obj})
def editcustomer(request,n):
"""
编辑
"""
old_obj = models.Customer.objects.filter(pk=n).first()
if request.method == 'GET':
book_form_obj = myforms.CustomerModelForm(instance=old_obj)
return render(request, 'customer/editcustomer.html', {'book_form_obj': book_form_obj})
else:
book_form_obj = myforms.CustomerModelForm(request.POST,instance=old_obj)
if book_form_obj.is_valid():
book_form_obj.save()
return redirect('customers')
else:
return render(request, 'customer/editcustomer.html', {'book_form_obj': book_form_obj})
和2为1
def addEditCustomer(request,n=None):
old_obj = models.Customer.objects.filter(pk=n).first()
label = '编辑页面' if n else '添加页面'
if request.method == 'GET':
book_form_obj = myforms.CustomerModelForm(instance=old_obj)
return render(request, 'customer/editcustomer.html', {'book_form_obj': book_form_obj,'label':label})
else:
book_form_obj = myforms.CustomerModelForm(request.POST,instance=old_obj)
if book_form_obj.is_valid():
book_form_obj.save()
return redirect('customers')
else:
return render(request, 'customer/editcustomer.html', {'book_form_obj': book_form_obj,'label':label})
html
editcustomer.html
{% extends 'starter.html' %}
{% block title %}
<h1>添加客户页面</h1>
{% endblock %}
{% block content %}
{% include 'customer/add_edit_mode.html' %}
o{% endblock %}
add_edit_mode.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-horizontal" action="" method="post" novalidate>
{% csrf_token %}
{% for field in book_form_obj %}
<div class="form-group">
<label for="{{ field.id_for_label }}" class="col-sm-2 control-label">{{ field.label }}</label>
<div class="col-sm-6">{{ field }}</div>
<div class="col-sm-2"><span class="text-danger">{{ field.errors.0 }}</span></div>
</div>
{% endfor %}
<button class="btn btn-success">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>