views:

22

answers:

1

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 searc

h(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  A: 

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'

knutin
Yes this what I was looking for thanks. If I was using numbers rather than normal characters would it work with startswith as well or something else. Let me explain an example. Suppose if I wanted to search for a number "1200" but I only wanted to type "12" to get "1200".
Shehzad009
If the field is a character field and you are searching for the string "12" instead of the number 12, then yes it would work. However, if the field is an integer field of some sort I do not know if using startswith will work, probably not.
knutin
Yes, that it what I said, because if there is an integer field that would probably not work. The only thing I can think of is hard code through the views to be able to do that, but that may be too complex.
Shehzad009
Just out of curiosity, why do you store it as an integer in the database, if you want to use it(ie. do searches) like a string?
knutin
This example that I have done yesterday. It was really an example following Django documentation website. Just really wanted to build my skills so in the future to perform partial search for a number in an IntegerField. From your previous comment, it does not look like there is an easy way.
Shehzad009