Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

search - get_next/prev_by after searching django

I have trouble after trying to use get_next_by_FOO/ get_previous_by_FOO after searching. For example, Search result gives 10 differences results. if I click the first, It shows information. Then I click next, it won't show the second result in 10 results, shows the next value in data. I know where is the issue In my code, contract get data from Model Contract, not from search result, that is why I click next it doesnt show the result, but I dont know to fix in views.py #def show contract information

def contract_detail(request, contract_id=None):
    contract = get_object_or_404(Contracts, contract=contract_id)
    try:
        the_next = contract.get_next_by_created_at()
    except:
        the_next=None

    try:
        the_prev = contract.get_previous_by_created_at()
    except:
        the_prev=None

    context = {
        "contract": contract,
        "the_next" : the_next,
        "the_prev": the_prev,
    }
    return render(request, "contract_detail.html", context)

#def show list contract after seaching and pagination

def list_contract(request):
    context = {}
    if request.method == 'GET':
        query1= request.GET.get('search1')
        query2= request.GET.get('search2')
        submitbutton= request.GET.get('submit')
        if query1 is not None:
            lookups_contract= Q(contract__icontains=query1)
            lookups_name = (Q(name__icontains=query2.upper())|Q(name__icontains=query2.lower()))
            results= Contracts.objects.filter(lookups_contract,lookups_name).distinct()

            contract_posts=results
            CONTRACT_POSTS_PER_PAGE = 10
            CONTRACT_POSTS_PER_PAGE = request.GET.get('CONTRACT_POSTS_PER_PAGE', CONTRACT_POSTS_PER_PAGE) or 10
            #pagination cho query search
            page = request.GET.get('page', 1)
            contract_posts_paginator = Paginator(contract_posts, CONTRACT_POSTS_PER_PAGE)
            try:
                contract_posts = contract_posts_paginator.page(page)
            except PageNotAnInteger:
                contract_posts = contract_posts_paginator.page(CONTRACT_POSTS_PER_PAGE)
            except EmptyPage:
                contract_posts = contract_posts_paginator.page(contract_posts_paginator.num_pages)

            context['contract_posts'] = contract_posts

            return render(request, "customer.html", context)
        else:
            contract_posts= Contracts.objects.all()
            CONTRACT_POSTS_PER_PAGE = 10
            CONTRACT_POSTS_PER_PAGE = request.GET.get('CONTRACT_POSTS_PER_PAGE', CONTRACT_POSTS_PER_PAGE) or 10

            #pagination cho phan ko search
            page = request.GET.get('page', 1)
            contract_posts_paginator = Paginator(contract_posts, CONTRACT_POSTS_PER_PAGE)
            try:
                contract_posts = contract_posts_paginator.page(page)
            except PageNotAnInteger:
                contract_posts = contract_posts_paginator.page(CONTRACT_POSTS_PER_PAGE)
            except EmptyPage:
                contract_posts = contract_posts_paginator.page(contract_posts_paginator.num_pages)

            context['contract_posts'] = contract_posts
            return render(request, 'customer.html', context)
    else:
        return render(request, 'customer.html')
question from:https://stackoverflow.com/questions/65644448/get-next-prev-by-after-searching-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...