Jake D
Jake D

Reputation: 61

Adding a simple search to a Django app

So I'm following this tutorial in order to search some of my models. However, there is a lack of documentation for what is provided and as someone new to Django, I'm confused as to what is missing in order to make this work.

So here's what I have:

EDIT

Revised the search template to include an input field to fetch the query.

myproject.templates.search.html:

<form action="" method="get">
<label for="id_q">Search:</label>
<input id="id_q" name="q" type="text">
<input type="submit" value="Submit">

{% if found_entries %}
    <p>You searched for "{{ query_string }}".</p>
    <ul>
        {% for i in found_entries %}
            {{ i.uid }} {{ i.title }} {{ value|linebreaks }}
        {% endfor %}
    </ul>
{% endif %}

{% if query_string and not found_entries %}
    <p>No results found.</p>
{% else %}
    <p>Type a search query into the box above, and press "Submit" to search.</p>
{% endif %}

</form>

myapp.models.py:

from django.db import models

class Book(models.Model):
    uid = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=30)
    class Meta:
        db_table = u'books'

myapp.search.py:

import re

from django.db.models import Q

def normalize_query(query_string,
                findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
                normspace=re.compile(r'\s{2,}').sub):
''' Splits the query string in invidual keywords, getting rid of unecessary spaces
    and grouping quoted words together.
    Example:

    >>> normalize_query('  some random  words "with   quotes  " and   spaces')
    ['some', 'random', 'words', 'with quotes', 'and', 'spaces']

'''
return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)] 

def get_query(query_string, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
    aims to search keywords within a model by testing the given search fields.

'''
query = None # Query to search for every search term        
terms = normalize_query(query_string)
for term in terms:
    or_query = None # Query to search for a given term in each field
    for field_name in search_fields:
        q = Q(**{"%s__icontains" % field_name: term})
        if or_query is None:
            or_query = q
        else:
            or_query = or_query | q
    if query is None:
        query = or_query
    else:
        query = query & or_query
return query

myapp.views.py:

from myapp.models import Book
from django.shortcuts import render_to_response
from django.template import RequestContext

def search(request):
    query_string = ''
    found_entries = None
    search_fields=('uid')

    if ('q' in request.GET) and request.GET['q'].strip():

        query_string = request.GET['q']

        entry_query = get_query(query_string, search_fields)

        found_entries = Book.objects.filter(entry_query)

        return render_to_response('search.html',
                         { 'query_string': query_string, 'found_entries': found_entries },
                         context_instance=RequestContext(request))

myproject.templates.search.html:

{% if found_entries %}
    <p>You searched for "{{ query_string }}".</p>
    <ul>
    {% for i in found_entries %}
        <li><a href="{{ q.get_absolute_url }}">{{ found_entries }}</a></li>
    {% endfor %}
    </ul>
{% endif %}
{% if query_string and not found_entries %}
    <p>No results found.</p>
{% else %}
    <p>Type a search query into the box above, and press "Submit" to search.</p>
{% endif %}

myproject.urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
url(r'^predictor/$', 'myapp.views.search'),
)

And here's what it looks like if I go to: http://localhost:8000/myapp/

image: https://i.sstatic.net/3IPRp.png

Thanks!

Upvotes: 0

Views: 8972

Answers (2)

hkothari
hkothari

Reputation: 254

If you take a look at your code (which I encourage you do) the view you're looking at is myapp.views.search right? Looking at your search function you should be able to see that it looks into request.GET for a 'q' key, which is equivelant to getting the 'q' element from the query string, so that's where it's getting your search from.

So to search you would go to http://localhost:8000/myapp/?q=searchterm where 'searchterm' refers to the term you're seaching for.

Like thomas said though you probably want to create a form to use this functionality also.

Upvotes: 1

Thomas Orozco
Thomas Orozco

Reputation: 55217

There is no form in your template: you never put a search box on the page, so why would one display?

Have a look at the django documentation on using forms.


This is going to require a fair amount of work, but you're not using any of Django's built-in machinery for creating forms. Doing so will make your code cleaner (and will be easier!).

Upvotes: 2

Related Questions