1.添加用户 和编辑可以写在一起
urls.py
url(r‘^customer_add/‘, customer.customer_change, name=‘customer_add‘), url(r‘^customer_edit/(\d+)/‘, customer.customer_change, name=‘customer_edit‘),
form.py
class BSForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)# 这2句相当于执行了父类的init方法 # 自定义操作 for fied in self.fields.values(): fied.widget.attrs.update({‘class‘: ‘form-control‘}) # 客户的form class CustomerForm(BSForm): class Meta: model = models.Customer#找到这个表 fields = ‘__all__‘## 所有字段 def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)#去掉course的form-control样式 self.fields[‘course‘].widget.attrs.pop(‘class‘)
views目录下 函数添加
def customer_change(request, edit_id=None): obj = models.Customer.objects.filter(pk=edit_id).first() # 处理POST if request.method == ‘POST‘: # 包含提交的数据 原始数据 form_obj = CustomerForm(request.POST, instance=obj) if form_obj.is_valid():#文件校验 form_obj.save() # 跳转到展示页面 return redirect(reverse(‘customer_list‘)) else: form_obj = CustomerForm(instance=obj)#也就是空的值 title = ‘编辑客户‘ if edit_id else ‘添加客户‘ return render(request, ‘customer_change.html‘, {‘title‘: title, ‘form_obj‘: form_obj})
customer_change.html
{% extends ‘layout.html‘ %} {% block content %} <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title">添加客户</h4> </div> <div class="panel-body"> <div class="col-lg-8 col-lg-offset-2 " style="margin-top: 10px"> <form class="form-horizontal" novalidate method="post"> {% csrf_token %} {% for field in form_obj %} <div class="form-group {% if field.errors %}has-error{% endif %}"> //选中报红的样式 <label for="{{ field.id_for_label }}"//字段名字 class="col-sm-2 control-label">{{ field.label }}</label> <div class="col-sm-10"> {{ field }} <span class="help-block"> {{ field.errors.0 }}</span> </div> </div> {% endfor %} <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">保存</button> </div> </div> </form> </div> </div> </div> {% endblock %}
2. 公户和私户的展示
# 展示公户 # url(r‘^customer_list/‘,customer.customer_list,name=‘customer_list‘), url(r‘^customer_list/‘,customer.CustomerList.as_view(),name=‘customer_list‘), # 展示私户 # url(r‘^my_customer/‘,customer.customer_list,name=‘my_customer‘), url(r‘^my_customer/‘,customer.CustomerList.as_view(),name=‘my_customer‘),
之前的函数
def customer_list(request):#公户私户显示 if request.path_info == reverse(‘customer_list‘): all_customer = models.Customer.objects.filter(consultant__isnull=True) else: all_customer = models.Customer.objects.filter(consultant=request.user_obj) return render(request, ‘customer_list.html‘, {‘all_customer‘: all_customer})
views/customer.py
# 展示客户列表 CBV from django.views import View class CustomerList(View): def get(self, request, *args, **kwargs): if request.path_info == reverse(‘customer_list‘): all_customer = models.Customer.objects.filter(consultant__isnull=True) else: all_customer = models.Customer.objects.filter(consultant=request.user_obj) page = Pagination(request.GET.get(‘page‘, 1), all_customer.count(), ) return render(request, ‘customer_list.html‘, { ‘all_customer‘: all_customer[page.start:page.end], ‘page_html‘: page.page_html }) def post(self, request, *args, **kwargs): action = request.POST.get(‘action‘) # multi_apply multi_pub # 判断是否有相应的操作 反射 if hasattr(self, action): # 有 获取并且执行 func = getattr(self, action) print(func) func() else: return HttpResponse(‘非法操作‘) return self.get(request, *args, **kwargs) def multi_apply(self, ): ids = self.request.POST.getlist(‘ids‘) # 把提交的客户的ID 都变成当前用户的私户 # 方式一 查询的客户 # models.Customer.objects.filter(pk__in=ids).update(consultant=self.request.user_obj) # models.Customer.objects.filter(pk__in=ids).update(consultant_id=self.request.session.get(‘pk‘)) # 方式二 查用户 self.request.user_obj.customers.add(*models.Customer.objects.filter(pk__in=ids)) def multi_pub(self): ids = self.request.POST.getlist(‘ids‘) # 把提交的客户的ID # 方式一 查询的客户 models.Customer.objects.filter(pk__in=ids).update(consultant=None) # 方式二 查用户 # self.request.user_obj.customers.remove(*models.Customer.objects.filter(pk__in=ids))
customer_list.html
{% extends ‘layout.html‘ %} {% block content %} <a class="btn btn-success btn-sm" style="margin: 3px" href="{% url ‘customer_add‘ %}"> <i class="fa fa-plus-square"></i> 添加 </a> <form action="" method="post" class="form-inline"> {% csrf_token %} <select name="action" id="" class="form-control"> {% if request.path_info == ‘/crm/my_customer/‘ %} <option value="multi_pub"> 私户变公户</option> {% else %} <option value="multi_apply"> 公户变私户</option> {% endif %} <option value="multi_del"> 批量删除</option> </select> <button class="btn btn-sm btn-primary">提交</button> <table class="table table-bordered table-hover"> <thead> <tr> <th>选择</th> <th>序号</th> <th>QQ</th> <th>姓名</th> <th>性别</th> {# <th>出生日期</th>#} {# <th>电话</th>#} <th>客户来源</th> <th>咨询课程</th> <th>状态</th> <th>最后跟进</th> <th>销售</th> <th>已报班级</th> <th>操作</th> </tr> </thead> <tbody> {% for customer in all_customer %} <tr> <td> <input type="checkbox" name="ids" value="{{ customer.pk }}"> </td> <td>{{ forloop.counter }}</td> <td>{{ customer.qq }}</td> <td>{{ customer.name|default:‘未填写‘ }}</td> <td>{{ customer.get_sex_display }}</td> {# <td>{{ customer.birthday|default:‘未填写‘ }}</td>#} {# <td>{{ customer.phone }}</td>#} <td>{{ customer.get_source_display }}</td> <td>{{ customer.course }}</td> <td> {{ customer.show_status }} </td> <td>{{ customer.last_consult_date }}</td> <td>{{ customer.consultant }}</td> {# <td>{{ customer.class_list.all }}</td>#} <td>{{ customer.show_class }}</td> <td> <a href="{% url ‘customer_edit‘ customer.pk %}"> <i class="fa fa-pencil-square-o"></i> </a> </td> </tr> {% endfor %} </tbody> </table> </form> {% endblock %}
原文地址:https://www.cnblogs.com/zaizai1573/p/10540308.html
时间: 2024-11-05 20:34:09