zoukankan      html  css  js  c++  java
  • django 项目 crm 关于展示表的细节

    1,def client(request,edit_id=None): #增加和修改
        obj = models.Customer.objects.filter(id=edit_id).first()
        form_obj = RegForm2(instance=obj)
        if request.method == 'POST':
            form_obj = RegForm2(request.POST,instance=obj)
            if form_obj.is_valid():
                form_obj.save()
                return redirect('/clients_library/')
        return render(request,'client.html',{'form_obj':form_obj})
    RegForms(instance=Ture) 这会保留原先的值进行操作
    2,反向操作的使用:
    在url中使用 name
    在html 中{% url 'url中的名' 值%}
    在views中 使用reverse
    3,对于相同代码的复用,这里使用了销售来做判断
    def customer_list(request):
        if request.path_info == reverse('my_customer'):
            all_customers = models.Customer.objects.filter(consultant=request.user).order_by('id')
        else:
            all_customers = models.Customer.objects.filter(consultant__isnull=True).order_by('id')
        return render(request, 'customer_list.html', {"all_customers": all_customers})
    4,选择框:
    思路:
    先由前端发出post的请求,发送选择的方式和具体的数据
    在由后端取出选择方式进行判断,再执行相应的代码,
    代码中会涉及到增删改查 
     
    add的使用 
    request.user.customers.remove(*models.Customer.objects.filter(id__in=obj_ids))
    对象打散 注意
    注意:
    request.user取到当前的登录的对象
    html代码:
    <form action="" method="post" class="form-inline">
        {% csrf_token %}
        <div class="form-group pull-left">
            <select name="actions" id="" class="form-control">
                <option value="">请选择</option>
                {% if request.path_info == '/clients_library/' %}
                    <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>
        </div> 
        <table class="table table-hover">
            <thead>
            <tr>
                <th>选择</th>
                <th>序号</th>
                <th>qq</th>
                <th>qq昵称</th>
                <th>姓名</th>
                <th>客户来源</th>
                <th>班级类型</th>
                <th>状态</th>
                <th>咨询日期</th>
                <th>最后跟进日期</th>
                <th>销售</th>
                <th>已报班级</th>
            </tr>
            </thead>
            <tbody>
            {% for cus in cus_filter %}
                <tr>
                    <td><input type="checkbox" value="{{ cus.id }}" name="id"></td>
                    <td>{{ forloop.counter }}</td>
                    <td><a href="{% url 'edit_client' cus.id %}">{{ cus.qq }}</a></td>
                    <td>{{ cus.qq_name|default:'暂无' }}</td>
                    <td>{{ cus.name|default:'暂无' }}</td>
                    <td>{{ cus.get_source_display }}</td>
                    <td>{{ cus.get_class_type_display }}</td>
                    <td>
                        {{ cus.show_status }}
                    </td>
                    <td>{{ cus.date }}</td>
                    <td>{{ cus.last_consult_date }}</td>
                    <td>{{ cus.consultant }}</td>
                    <td>{{ cus.show_class }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
     
    </form>
    views:
    def post(self,request):
        action = request.POST.get('actions')
        if not hasattr(self,action):
            return HttpResponse('非法操作')
        getattr(self,action)(request)
        return self.get(request)
    def multi_apply(self,request):
        obj_ids = request.POST.getlist('id')
        print(self.request.user)
        request.user.customers.remove(*models.Customer.objects.filter(id__in=obj_ids))
    def multi_pub(self,request):
        obj_ids = request.POST.getlist('id')
        request.user.customers.add(*models.Customer.objects.filter(id__in=obj_ids))
    def multi_del(self,request):
        obj_ids = request.POST.getlist('id')
        models.Customer.objects.filter(id__in=obj_ids).delete()
     
     
     
    5,搜索框:加入数据库选择的情况,需要注意分页的查看
    Q方法的使用:
    q = Q()
    q.connector = 'OR'
    q.children.append()
    Q(Q()|Q())
    Q(Q('f_contains',值))
    QueryDict转换:
    query_params = deepcopy(request.GET)
    query_params._mutable = True 允许添加,默认为False
    self.query_params.urlencode()将字典转化为&连接
    6,分页加搜索框:
    代码:
    """
    分页组件
    """
    from django.utils.html import mark_safe
     
     
    class Pagination(object):
     
        def __init__(self, request, all_count, base_url, query_params, per_num=10, max_show=11):
            try:
                current_page = int(request.GET.get('page'))
                if current_page <= 0:
                    raise Exception()
            except Exception as e:
                current_page = 1
            self.base_url = base_url
            self.current_page = current_page
            self.query_params = query_params
            self.max_show = max_show
            self.half_show = max_show // 2
            self.all_count = all_count
            self.per_num = per_num
            self.total_page, more = divmod(self.all_count, self.per_num)
            if more:
                self.total_page += 1
     
        @property
        def start(self):
            return (self.current_page - 1) * self.per_num
     
        @property
        def end(self):
            return self.current_page * self.per_num
     
        @property
        def html_str(self):
            # 计算起始页码数和终止页码数
            # 总页码数小于最大显示的页码数
            if self.total_page < self.max_show:
                page_start = 1
                page_end = self.total_page
            else:
                if self.current_page <= self.half_show:
                    # 总页码数大于最大显示页码数
                    page_start = 1
                    page_end = self.max_show
                elif self.current_page + self.half_show > self.total_page:
                    page_start = self.total_page - self.max_show + 1
                    page_end = self.total_page
                else:
                    page_start = self.current_page - self.half_show
                    page_end = self.current_page + self.half_show
            html_list = []
            if self.current_page <= 1:
                prev_li = '<li class="disabled"><a><上一页></a></li>'
            else:
                self.query_params['page'] = self.current_page - 1
                prev_li = '<li><a href="{0}?{1}"><上一页></a></li>'.format(self.base_url, self.query_params.urlencode())
            html_list.append(prev_li)
            for i in range(page_start, page_end + 1):
                self.query_params['page'] = i
                if i == self.current_page:
                    li_html = '<li class="active"><a href="{0}?{1}">{2}</a></li>'.format(self.base_url,
                                                                                         self.query_params.urlencode(), i)
                else:
                    li_html = '<li><a href="{0}?{1}">{2}</a></li>'.format(self.base_url, self.query_params.urlencode(), i)
     
                html_list.append(li_html)
     
            if self.current_page >= self.total_page:
                next_li = '<li class="disabled"><a>< 下一页 ></a></li>'
            else:
                self.query_params['page'] = self.current_page + 1
                next_li = '<li><a href="{0}?{1}">< 下一页 ></a></li>'.format(self.base_url, self.query_params.urlencode())
            html_list.append(next_li)
     
            return mark_safe("".join(html_list))
     
     
     
    使用:
      page_obj = Pagination(request, len(all_customers), request.path_info,query_params, 2)
     
            return render(request, 'customer_list.html',
                          {
                              "all_customers": all_customers[page_obj.start:page_obj.end],
                              "page_html": page_obj.html_str
                          })
     
     
     
     
    class CustomerList(View):
     
        def get(self, request):
     
            q = self.get_search_conditon(request, ['qq', 'qq_name'])
     
            if request.path_info == reverse('my_customer'):
                all_customers = models.Customer.objects.filter(consultant=request.user).order_by('id')
            else:
                all_customers = models.Customer.objects.filter(
                    q, consultant__isnull=True).order_by(
                    'id')
            query_params= copy.deepcopy(request.GET)
            # <QueryDict: {'query': ['456']}>
            # ?query=456
            query_params._mutable = True
            # query_params['page']=15
            # print(query_params.urlencode())
            # ?query =456&page=15
            # 处理分页
            page_obj = Pagination(request, len(all_customers), request.path_info,query_params, 2)
     
            return render(request, 'customer_list.html',
                          {
                              "all_customers": all_customers[page_obj.start:page_obj.end],
                              "page_html": page_obj.html_str
                          })
     
        def post(self, request):
            action = request.POST.get('actions')
            if not hasattr(self, action):
                return HttpResponse('非法操作')
            getattr(self, action)(request)
            return self.get(request)
     
        def multi_apply(self, request):
            obj_ids = request.POST.getlist('id')
            request.user.customers.add(*models.Customer.objects.filter(id__in=obj_ids))
     
        def multi_pub(self, request):
            obj_ids = request.POST.getlist('id')
            request.user.customers.remove(*models.Customer.objects.filter(id__in=obj_ids))
     
        def get_search_conditon(self, request, fields_list):
            query = request.GET.get('query', '')
            q = Q()
            q.connector = 'OR'
            for i in fields_list:
                q.children.append(Q(('%s__contains' % i, query)))
            return q
     

  • 相关阅读:
    Discuz X 2.5 点点(伪静态)
    jq 、xml 省市级联动
    php memcache 初级使用(2)
    关于windows虚拟内存管理的页目录自映射
    SharePoint 2010 网络上的开发经验和资源
    SharePoint 2010 Reporting Services 报表服务器正在内置 NT AUTHORITY\SYSTEM 账户下运行 解决方法
    SharePoint 2010 Reporting Services 报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥 解决方法
    Active Directory Rights Management Services (AD RMS)无法检索证书层次结构。 解决方法
    SharePoint 2010 Reporting Services 报表服务器实例没有正确配置 解决方法
    SharePoint 2010 页面引用 Reporting Services 展现 List 报表
  • 原文地址:https://www.cnblogs.com/lnrick/p/9754139.html
Copyright © 2011-2022 走看看