CRM开发报名流程
在客户信息里点击报名进入报名页面,销售选择好报名的大致信息点击下一步会生成一个链接给客户填写,如果该用户报名已存在也会生成一条链接
在crm/urls.py文件中设置报名url
url(r\'^customer/(\d+)/enrollment/$\', views.enrollment, name="enrollment"
在crm/views.py文件中创建enrollment函数
因为在报名页面里面的数据需要从报名表里面得出来,所以得先获取表单里面的值,所以新建crm/forms.py
from django.forms import ModelForm
from PerfectCRM.Perfectcrm.crm import models
class EnrollmentForm(ModelForm):
def __new__(cls,*args,**kwargs):
#super(CustomerForm,self).__new__(*args,**kwargs)
# print("request.POST:",request.POST)
#表名,表对象值
for field_name,field_obj in cls.base_fields.items():
#print(field_name,dir(field_obj))
#给表输入框添加class
field_obj.widget.attrs[\'class\'] = \'form-control\'
return ModelForm.__new__(cls)
class Meta:
model = models.Enrollment
#表单展示的字段
fields = ["enrolled_class","consultant"]
在crm/views.py获取EnrollmentForm类并返回给前端,后端处理请求的数据
from django.shortcuts import render
from PerfectCRM.Perfectcrm.crm.forms import EnrollmentForm
from PerfectCRM.Perfectcrm.crm import models
from django.db import IntegrityError
def enrollment(request,customer_id):
#根据请求过来的customet_id获得该id的客户对象
customer_obj = models.Customer.objects.get(id=customer_id)
msgs={}
if request.method == "POST":
#把请求的数据放到表单里面
enroll_form = EnrollmentForm(request.POST)
#表单里面的是有效的
if enroll_form.is_valid():
msg=\'\'\'请将此链接发送给客户进行填写:
http://localhost:8000/crm/customer/registration/{enroll_obj_id}/\'\'\'
try:
print("cleaned_data",enroll_form.cleaned_data)
#在enroll表单中加上前端的客户请求的值,因为客户的名称是从customer中获取,在enroll表中是没有的,会报错所以才在enroll中的customer添加他的值
enroll_form.cleaned_data["customer"]= customer_obj
#创建表单的值报名
enroll_obj = models.Enrollment.objects.create(**enroll_form.cleaned_data)
#报名生成的链接
msgs["msg"] = msg.format(enroll_obj_id=enroll_obj.id)
#捕捉当用户已存在的错误
except IntegrityError as e:
#如果是改用户报名之前已经存在,那么报名的id就是要去customer表中找,和enroll两个表中找关联的
enroll_obj =models.Enrollment.objects.get(customer_id=customer_obj.id,
enrolled_class_id = enroll_form.cleaned_data["enrolled_class"].id)
enroll_form.add_error("__all__","该用户已存在")
msgs["msg"] = msg.format(enroll_obj_id=enroll_obj.id)
else:
enroll_form = EnrollmentForm()
return render(request,"sales/enrollment.html",{"enroll_form":enroll_form,
"customer_obj":customer_obj,
"msgs":msgs})
在templates/sales/enrollment.html新建处理前端的页面
{% extends \'index.html\'%}
{% block page-content %}
<div id="content">
<form class="form-horizontal col-lg-7" role="form" method="post">{%csrf_token%}
<span style="color: red">{{enroll_form.errors}}</span>
<div class="form-group form-group-lg">
<label class="col-sm-2 control-label">客户</label>
<div class="col-sm-10">
{{customer_obj.qq}}
</div>
</div>
{% for field in enroll_form %}
<div class="form-group form-group-lg">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-10">
{{field}}
</div>
</div>
{% endfor %}
<input type="submit" class="btn btn-info pull-right" value="下一步">
</form>
<div class="row">
<ul>
{% for k,v in msgs.items%}
<li>{{k}}:{{v}}</li>
{% endfor %}
</ul>
</div>
</div>
{% endblock%}