Download django-search apps
http://code.google.com/p/django-search
Extract and copy-paste /mysite/djangosearch
Open settings.py
SEARCH_ENGINE = “mysql”
Open models.py
from djangosearch import search
from djangosearch.paginator import SearchPaginator
from djangosearch.indexer import get_indexed_models, ModelIndex
write the index for those classes on which you want to implement search functionality
for example
class city(models.Model):
name = models.CharField(max_length = 100)
code = models.CharField(max_length = 50)
active_flag = models.BooleanField()
rank = models.CharField(max_length=15, choices=RANK_CHOICES)
index = djangosearch.ModelIndex(text=['name'], additional=[])
def __unicode__(self):
return self.name
def get_search(self):
return “%s” % self.name
Open urls.py
(r’^search/$’,'view.searching’),
Open views.py
import djangosearch
from djangosearch import ModelIndex
from djangosearch.backends.mysql import *
from djangosearch import search
from djangosearch.paginator import SearchPaginator
from djangosearch.indexer import get_indexed_models, ModelIndex
def searching(request):
query1 = request.POST['SearchText']
results_city = city.index.search(query1)
return render_to_response(“search.html”, {‘results_city’ : results_city})
Open search.html
<div>Searched Results</div>
{%if results_city%}
{% for n in results_results_city%}
<div style=”font-color:green”><a href=”{{n.get_absolute_url}}”>{{n}}</a></div>
<div> {{n.get_search}} </div>
{% endfor %}
{% else %} Not Found..
{% endif %}
For any query please write a comment with valid email id.
Thanks
