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 564209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:44:33+00:00 2026-05-13T12:44:33+00:00

This view works when the query is in the database. def search(request): if ‘q’

  • 0

This view works when the query is in the database.

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
        q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
        lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=q)        
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')

So, if q=delelle then it finds other lawyers in the database who graduated from the same school the same year.

if q=collins and collins is in the database but there are no other lawyers who graduated from the same school the same year then it gives appropriate error message “No lawyers matched your search criteria.”

But if q=moritz and there is no lawyer named moritz in the database then it gives a 500 internal server error.

I don’t understand the request.GET['q'] notation or how I can fix this so that I can add the proper text for the case when the query is not in the database. Can you please point me in the right direction? Thanks.

Edit re Antony Hatchkins answer

The below code works without giving the error. I will try to incorporate the rest soon:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        lawyer = Lawyer.objects.filter(last__icontains=q)
        if len(lawyer)==0:
            return render_to_response('not_in_database.html', {'query': q})
        else:
            q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
            q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
            lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=q)           
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')
  • 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-05-13T12:44:34+00:00Added an answer on May 13, 2026 at 12:44 pm
    def search(request):
        q = request.GET.get('q', '')
        if q:
            lawyers = Lawyer.objects.filter(last__icontains=q)
            if len(lawyers)==0:
                return HttpResponse('No such lawyer')
            if len(lawyers)>1:
                return HttpResponse('Several lawyers matched')
            lawyers1 = Lawyer.objects.filter(school=lawyers[0].school).filter(year_graduated=lawyers[0].year).exclude(pk=lawyers[0].pk)        
            return render_to_response('search_results.html', {'lawyers': lawyers1, 'query': q})
        else:
            return HttpResponse('Please submit a search term.')
    

    request.GET['q'] fetches field q from client browser’s GET request object

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

Sidebar

Related Questions

In my view controller's -viewDidLoad method, I call [myTextField becomeFirstResponder]; This works like a
View this code: function testprecision(){ var isNotNumber = parseFloat('1.3').toPrecision(6); alert(typeof isNotNumber); //=> string }
My create view code like this create VIEW [dbo].[vw_test] AS SELECT 'B' AS rtype,
Recent conversations with colleagues have produced varying points of view on this matter. What
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
This is what i'm trying to achieve: Home (Table View) > (Click on row)
I have this form in my view: <!-- Bug (extra 'i') right here-----------v -->
I am asking this question from an educational/hacking point of view, (I wouldn't really
According to this: http://www.codeplex.com/IronPython/Wiki/View.aspx?title=IP20VsCPy25Perf&referringTitle=IronPython%20Performance IronPython (Python for .Net) is faster than regular Python (cPython)

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.