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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:18:05+00:00 2026-06-06T06:18:05+00:00

I have a django view that searches my database for a name that includes

  • 0

I have a django view that searches my database for a name that includes the text a user submitted on a form.

When i use .filter to search i check the users query against the first_name and last_name fields of each record. It all works but my problem is, if a user entered the full name in the search box with whitespace (eg. “John Smith” instead of “john” or “smith” my function would return no results!

Quite new to all this and I’m not sure how i would go about changing the function or even the form. I could just be lazy and prevent them from entering the space key (I presume that’s possible) or something but i’d like to learn the actual solution to my problem?

Here’s the form and the view, they’re pretty simple:

<form action="/Speakers/Search" method="get">
    <input type="text" name="q">
    <input type="submit" value=" Search ">
    </form>

Complete side question: Just realised, because i have been using a text editor with line-wrap, i forgot i still don’t know where it’s “safe” to add things line line-breaks in python? is it only the indentation that matters..? So apologies for having to scroll the code below:

def SearchSpeakers(request):
    if 'q' in request.GET and request.GET['q']: #2nd condition return false if emtpy string
        search = request.GET['q']
        message = "You searched for: %s." % search
        results = Speaker.objects.filter(Q(first_name__icontains=search) | Q(last_name__icontains=search))
        if not results: #returned empty
            message += " We could not find the name you entered in our Speakers List but you check back again before the event! Press clear to see the complete list again."

        return render_to_response('Speakers.html', {'speakers':results, 'query': message})
    else:
        message = "You did not enter a name."
        return render_to_response('Speakers.html',{'query':message})
  • 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-06T06:18:06+00:00Added an answer on June 6, 2026 at 6:18 am

    It looks like your issue is that you are searching the first_name or last_name field with the whole_name. You could add some logic that checks the input (splits on whitespace or comma’s) and searches each member of the whole name in a loop. I’m not familiar with Django but

    search = request.GET['q']
    message = "You searched for: %s." % search
    for term in list(set(search.split())):
        # ...search each term and compile into a set of results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which displays a form that a logged-in user can use
I have a django view that looks like... def add_user(request): if User.objects.get(username__exact = request.POST['username']):
I have a working django form that submits and the django view handles the
I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have a simple Django view that just returns URL parameters, but if I
I have the view function in django that written like a dispatcher calling other
I have a Django view that receives POSTs which do not need to have
Suppose you have a Django view that has two functions: The first function renders
Still new to python and django, though learning ;-) I have a view that
I have a Django view that returns a list of dicts like so data

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.