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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:20:06+00:00 2026-05-17T21:20:06+00:00

Hello I am working in django how to use a search form from Django

  • 0

Hello I am working in django how to use a search form from Django documents site. Here are some information below.
Now, is there a way I can produce a part search for title? For example if I wanted to search for a book called “Apress”, But instead of typing the whole word I just wrote “ap” to get Apress. Is there a way how to achieve this solution?

    from django.db.models import Q
    from django.shortcuts import render_to_response
    from models import Book

    #views.py
    def search(request):
        query = request.GET.get('q', '')
        if query:
            qset = (
                Q(title__icontains=query) |
                Q(authors__first_name__icontains=query) |
                Q(authors__last_name__icontains=query)
            )
            results = Book.objects.filter(qset).distinct()
        else:
            results = []
        return render_to_response("books/search.html", {
            "results": results,
            "query": query
        })

    #search html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
        <title>Search{% if query %} Results{% endif %}</title>
    </head>
    <body>
      <h1>Search</h1>
      <form action="." method="GET">
        <label for="q">Search: </label>
        <input type="text" name="q" value="{{ query|escape }}">
        <input type="submit" value="Search">
      </form>

      {% if query %}
        <h2>Results for "{{ query|escape }}":</h2>

        {% if results %}
          <ul>
          {% for book in results %}
            <li>{{ book|escape }}</l1>
          {% endfor %}
          </ul>
        {% else %}
          <p>No books found</p>
        {% endif %}
      {% endif %}
    </body>
    </html>
  • 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-17T21:20:07+00:00Added an answer on May 17, 2026 at 9:20 pm

    If you want to match on the beginning of a field, you can use startswith or istartswith if you want it to be case sensitive. icontains which you are using now will allow matches even within strings, ie. ‘arry’ will match ‘Harry’. While startswith will allow ‘Har’ to match ‘Harry’, but not ‘ArHarHar’

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

Sidebar

Related Questions

hello is there a way to import data from excel 2007 file? it is
FIXED : SEE BELOW ok. so here's my app. http://libwiki.heroku.com/libraries if you view it
[14:27: /mnt/mydocuments/git_working_dir/cat_example$] catalyst.pl hello_world Couldn't load class (Catalyst::Exception::Basic) because: Can't use an undefined value
Hello guys and thanks for your help as always. I am stuck on Asp.NET
I've just started using Ruby on Rails and so far it's working nicely. I'm
i'm trying to develop a gnome shell extension and I've created the Hello World
What I'm trying to achieve is the following. I'm (still) working on a timesheet,
I am trying out nservicebus as a solution instead of using WCF MSMQ binding.
I have managed to track done a weird problem in an init script I
I would like to write an algorithm that compares one memory block with another

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.