• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Django CRM项目(公户转私户、模糊查询、分页保留搜索条件、编辑后跳转至源页面)day19

开发技术 开发技术 4小时前 1次浏览

内容回顾

展示客户信息

  1. 普通字段

  2. choice参数

    • 对象.字段 —>数据库中的值
    • 对象.get_字段_display()—>中文的提示
  3. 外键

    • 对象.外键 —>所关联的对象 __str__
    • 对象.外键.name
  4. 自定义方法

    • 多对多:

      class Customer():
          def show_class(self):
              
              return '|'.join([ str(i) for i in self.class_list.all() ])
      
    • HTML:

      from django.utils.safestring import mark_safe
      def show_status(self):
      
      
          color_dict = {
              'signed': 'green',
              'unregistered': 'red',
              'studying': 'blue',
              'paid_in_full': 'yellow',
          }
      
          return mark_safe('<span style="background-color: {};color: white;padding: 3px">{}</span>'.format(
              color_dict.get(self.status),
              self.get_status_display()))
      

新增和编辑客户信息

  • modelform

    def customer_change(request, edit_id=None):
        obj = models.Customer.objects.filter(pk=edit_id).first()
        form_obj = CustomerForm(instance=obj)
        if request.method == 'POST':
            form_obj = CustomerForm(request.POST, instance=obj)
            if form_obj.is_valid():
                form_obj.save()  # 没有instance新增  有instance做修改
                return redirect(reverse('customer_list'))
        return render(request, 'customer_change.html', {'form_obj': form_obj, 'edit_id': edit_id})
    
    

公户和私户的展示

  • filter()
# 客户展示
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.account)

    return render(request, 'customer_list.html', {'all_customer': all_customer})

今日内容

公户和私户的转换

什么是公户?

​ 客户没有绑定销售,就是公户

什么是私户?

​ 客户绑定销售,就是某个销售的私户

class CustomerList(View):

    def get(self, request):

        q = self.search(['qq', 'name', ])

        if request.path_info == reverse('customer_list'):
            all_customer = models.Customer.objects.filter(q, consultant__isnull=True, )
        else:
            # 私户
            all_customer = models.Customer.objects.filter(q, consultant=request.account)

        pager = Pagination(request.GET.get('page', '1'), all_customer.count(), request.GET.copy(), 2)
        return render(request, 'customer_list.html', {
            'all_customer': all_customer[pager.start: pager.end],
            'page_html': pager.page_html
        })

    def post(self, request):
        action = request.POST.get('action')

        if not hasattr(self, action):
            return HttpResponse('非法操作')

        getattr(self, action)()

        return self.get(request)

    def multi_apply(self):
        # 公户变私户
        ids = self.request.POST.getlist('id')
        # 方式一
        models.Customer.objects.filter(id__in=ids).update(consultant=self.request.account)

        # 方式二
        # self.request.account.customers.add(*models.Customer.objects.filter(id__in=ids))

    def multi_public(self):
        ids = self.request.POST.getlist('id')
        # 方式一
        models.Customer.objects.filter(id__in=ids).update(consultant=None)
        # 方式二
        # self.request.account.customers.remove(*models.Customer.objects.filter(id__in=ids))

    def search(self, query_list):
        query = self.request.GET.get('query', '')

        # Q(Q(qq__contains=query) | Q(name__contains=query))
        q = Q()
        q.connector = 'OR'

        #  Q(('qq__contains', query))    Q(qq__contains=query)
        for i in query_list:
            q.children.append(Q(('{}__contains'.format(i), query)))

        return q

模糊查询

q = Q()
q.connector = 'OR'
q.children.append(Q(qq__contains=query))
Q(Q('qq__contains',query)		Q(Q(qq__contains=query)	

分页保留搜索条件

request.GET    # query:1
request.GET.urlencode()   # 'query=1'
request.GET._mutable = True   # 可修改
request.GET['page'] = 1  # query:1  page : 1 
request.GET.urlencode()   # 'query=1&pagr=1'

request.GET.copy()  # 深拷贝,可以修改

编辑后跳转至源页面

###templatetags/my_tags.py

from django import template
from django.urls import reverse
from django.http.request import QueryDict
register = template.Library()

@register.simple_tag
def reverse_url(request,name,*args,**kwargs):
    #获取当前的访问的路径
    next = request.get_full_path()
    print(next)
    url = reverse(name,args=args,kwargs=kwargs)
    qd = QueryDIct(mutable=True)
    qd['next'] = next
    
    return_url = "{}?{}".format(url,qd.urlencode())
    return return_url

喜欢 (0)