Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:50:35+00:00 2026-06-18T01:50:35+00:00

I am trying to build a relatively simple search form that allows the user

  • 0

I am trying to build a relatively simple search form that allows the user to search one of three search fields or any combination of the three. The result should filter the queryset based upon the terms given. Here is my form from search_form.html:

<form class="form-horizontal" action="/search/" method="get">
    <legend>Generate a Quiz:</legend>
    <div class="control-group">
        <label class="control-label" for="q">Search term</label>
        <div class="controls">
            <input type="text" name="q" placeholder="Search term" autocomplete="on"/>
        </div>
    </div>  
    <div class="control-group">
        <label class="control-label" for="yr">Year</label>
        <div class="controls">
            <input type="text" name="yr" placeholder="Year" autocomplete="on"/>
        </div>
    </div>  
    <div class="control-group">
        <label class="control-label" for="ct">Category</label>
        <div class="controls">
            <input type="text" name="ct" placeholder="Category" autocomplete="on"/>
        </div>
    </div>  
    <div class="controls">
        <button type="submit" class="btn btn-primary">Fetch  &raquo;</button>
    </div>          
</form>

And here is my view:

def search(request):
    error = False
    if 'q' in request.GET:
        q = request.GET['q']
        yr = request.GET['yr']
        ct = request.GET['ct']
        if not ((q or yr) or ct):
            error = True
        else:
            question_set = Uroquiz.objects.filter(question__icontains=q).filter(year=yr).filter(cat_icontains=ct)
            paginator = Paginator(question_set, 1)
            page = request.GET.get('page')

            try:
                question_set = paginator.page(page)
            except PageNotAnInteger:
                question_set = paginator.page(1)
            except EmptyPage:
                question_set = paginator.page(paginator.num_pages)
            return render_to_response('search_results.html',
                {'question': question_set, 'query': q, 'year': yr, 'ct': ct})
    return render_to_response('search_form.html', {'error': error})

Problems:

  1. When I built this with just the ‘q’ filed, it worked fine. When I chained in the ‘yr’ field to my filter, it works fine as long as there is a year entered into the form. If year (an integer field in my database) was empty, it would raise a ValueError invalid literal for int() with base 10: ”.

  2. When I chained in the ‘ct’ field, and try to search all fields, I get the error: Cannot resolve keyword ‘cat_icontains’ into field. I can change the filter to (cat=ct) and it works fine, but I should be able to use _icontains on the ‘cat’ field from my database.
    I’m sure I am missing something basic, any help is appreciated.

Update:
Here is my current solution based on the answers below, that allows searching on any combination of fields:

def search(request):
    q = request.GET.get('q')
    yr = request.GET.get('yr', 0)
    ct = request.GET.get('ct')
    question_set = Uroquiz.objects.all()

    if q:
        if not (yr or ct):
            question_set = Uroquiz.objects.filter(question__icontains=q)
    if yr:
        if not (q or ct):
            question_set = Uroquiz.objects.filter(year=yr)
    if ct:
        if not (q or yr):
            question_set = Uroquiz.objects.filter(cat__icontains=ct)
    if (q and yr):
        if not ct:
            question_set = Uroquiz.objects.filter(question__icontains=q).filter(year=yr)
    if (q and ct):
        if not yr:
            question_set = Uroquiz.objects.filter(question__icontains=q).filter(cat__icontains=ct)
    if (yr and ct):
        if not q:
            question_set = Uroquiz.objects.filter(year=yr).filter(cat__icontains=ct)
    if ((q and yr) and ct):
        question_set = Uroquiz.objects.filter(question__icontains=q).filter(year=yr).filter(cat__icontains=ct)
    if not ((q or yr) or ct):
        return render_to_response('search_form.html')
    paginator = Paginator(question_set, 1)
    page = request.GET.get('page')
    try:
        question_set = paginator.page(page)
    except PageNotAnInteger:
        question_set = paginator.page(1)
    except EmptyPage:
        question_set = paginator.page(paginator.num_pages)
    return render_to_response('search_results.html',
        {'question': question_set, 'query': q, 'yr': yr, 'ct': ct})

This seems to be a ‘busy’ way to solve this. Is there a more idiomatic/pythonic way of accomplishing this search result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:50:36+00:00Added an answer on June 18, 2026 at 1:50 am

    It is not guarenteed that a user will submit q, yr, and ct. You cannot trust your users data. If a user didn’t submit any of those params i think it will result in an error KeyError

    you could set default for these using get

        q = request.GET.get('q')
        yr = request.GET.get('yr', 0)
        ct = request.GET.get('ct')
    

    Also you could build your queryset as each param is found. remember querysets are laziy

    question_set = Uroquiz.objects.all()    
    q = request.GET.get('q')
    if q:
      question_set = question_set.filter(question__icontains=q)
    etc
    

    for your second question, I believe, it is syntax related cat__icontains 2 underscores (instead of filter(cat_icontains=ct), like you have in your code)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a relatively simple solution to deploy a website that allows
I am trying to build a relatively simple named scope in my Products class.
I have a simple image rotator script that I'm trying to build but I
I'm trying to build a relatively simple game review site. There should be a
I am trying to build a simple app that will show a bidimensional, scrollable
I'm trying build a method which returns the shortest path from one node to
Im trying to build an instant messenger in facebook styled bar that sits at
Im trying to build a simple faye realtime based notification system so I can
I'm trying to build a simple hangouts app which uses a canvas. The issue
I am trying to build a simple div based popup which can be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.