有没有一种简单的方法可以将所有输入集中在一行中?

时间:2019-12-17 05:42:37

标签: django django-views

search = True if (
        request.POST.get('name') or request.POST.get('city') or
        request.POST.get('email') or request.POST.get('tag') or
        request.POST.get('status') or
        request.POST.get('source') or
        request.POST.get('assigned_to')
    ) else False

我尝试了request.POST.get('name',..... etc)

但是它现在正在工作。我可以在一个地方分配全部吗?

views.py的完整代码

def lead_list_view(request):

queryset = Lead.objects.all().exclude(status='converted'
    ).select_related('created_by'
    ).prefetch_related('tags', 'assigned_to',)
if request.user.role == 'ADMIN' or request.user.is_superuser:
    queryset = queryset
else:
    queryset = queryset.filter(
        Q(assigned_to__in=[request.user]) |
        Q(created_by=request.user))

if request.method == 'GET':
    context = {}
    if request.GET.get('tag', None):
        queryset = queryset.filter(tags__in = request.GET.getlist('tag'))

    open_leads = queryset.exclude(status='closed')
    close_leads = queryset.filter(status='closed')

    context["status"] = LEAD_STATUS
    context["open_leads"] = open_leads
    context["close_leads"] = close_leads
    context["per_page"] = request.POST.get('per_page')
    context["source"] = LEAD_SOURCE

    context["users"] = User.objects.filter(
        is_active=True).order_by('email').values('id', 'email')

    tag_ids = list(set(queryset.values_list('tags', flat=True,)))
    context["tags"] = Tags.objects.filter(id__in=tag_ids)
    return render(request, 'leads.html', context)

if request.method == 'POST':
    context = {}
    search = True if (
        request.POST.get('name') or request.POST.get('city') or
        request.POST.get('email') or request.POST.get('tag') or
        request.POST.get('status') or
        request.POST.get('source') or
        request.POST.get('assigned_to')
    ) else False
    context["search"] = search

    request_post = request.POST
    if request_post:
        if request_post.get('name'):
            queryset = queryset.filter(
                Q(first_name__icontains=request_post.get('name')) or
                Q(last_name__icontains=request_post.get('name')))
        if request_post.get('city'):
            queryset = queryset.filter(
                city__icontains=request_post.get('city'))
        if request_post.get('email'):
            queryset = queryset.filter(
                email__icontains=request_post.get('email'))
        if request_post.get('status'):
            queryset = queryset.filter(status=request_post.get('status'))
        if request_post.get('tag'):
            queryset = queryset.filter(tags__in=request_post.getlist('tag'))
        if request_post.get('source'):
            queryset = queryset.filter(source=request_post.get('source'))
        if request_post.getlist('assigned_to'):
            queryset = queryset.filter(
                assigned_to__id__in=request_post.getlist('assigned_to'))
    queryset = queryset.distinct()

    open_leads = queryset.exclude(status='closed')
    close_leads = queryset.filter(status='closed')

    context["status"] = LEAD_STATUS
    context["open_leads"] = open_leads
    context["close_leads"] = close_leads
    context["per_page"] = request.POST.get('per_page')
    context["source"] = LEAD_SOURCE

    context["users"] = User.objects.filter(
        is_active=True).order_by('email').values('id', 'email')

    tag_ids = list(set(queryset.values_list('tags', flat=True,)))
    context["tags"] = Tags.objects.filter(id__in=tag_ids)

    context["assignedto_list"] = [
        int(i) for i in request.POST.getlist('assigned_to', []) if i]
    context["request_tags"] = request.POST.getlist('tag')

    tab_status = 'Open'
    if request.POST.get('tab_status'):
        tab_status = request.POST.get('tab_status')
    context['tab_status'] = tab_status
    return render(request, 'leads.html', context)
    # return context

2 个答案:

答案 0 :(得分:2)

你可以喜欢

inputs =  ['name','city','email','tag','status','source','assigned_to']    
search = inputs in request.POST.keys() #boolean

答案 1 :(得分:1)

要检查所有存在的输入参数并具有值或满足其他要求:

// file_history_button
$('body').on('click', '.file_history_button', function (e) {
    const file_name = $(this).siblings().find('input').val();
    alert("file_name : ", file_name);
});

inputs = ['name','city','email','tag','status','source','assigned_to'] search = all (bool(request.POST.get(input)) for input in inputs) 支票可以替换为任何其他必需的支票。


仅检查请求中是否存在必需的参数,而忽略它们的值:

bool()

inputs =  ['name', 'city', 'email', 'tag', 'status', 'source', 'assigned_to']
params = request.POST.keys()
search = all(input in params for input in inputs)