Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Django-search(Full Text)

Posted by Praveen Kumar on March 7, 2009

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: