Changes between Initial Version and Version 1 of searchengine


Ignore:
Timestamp:
Aug 14, 2006, 3:28:32 PM (18 years ago)
Author:
riklaunim@…
Comment:

Page Creation

Legend:

Unmodified
Added
Removed
Modified
  • searchengine

    v1 v1  
     1This tutorial will show you the basic of Django when we will make a google search engine front end in the framework. I've googled out an interesting python module called [http://oregonstate.edu/~barnesc/web_search/ web_search] which allows to get search results from few search engines and dmoz.
     2
     3- Download  '''web_search.py''' from that page
     4- Create a new project called “google”
     5
     6{{{
     7django-admin.py startproject google
     8}}}
     9- Create a new application called "searchengine":
     10{{{
     11python manage.py startapp searchengine
     12}}}
     13- Put '''web_search.py''' file in '''searchengine''' folder
     14- Create '''templates''' folder in the main project folder
     15- Edit '''settings.py''' and set TEMPATES_DIR to:
     16{{{
     17TEMPLATE_DIRS = (
     18    'templates/'
     19)
     20}}}
     21- Start the development server:
     22{{{
     23python manage.py runserver 8080
     24}}}
     25
     26We have preconfigured django project that is ready for the search engine code. We will have to make a template with a form where we will be able to enter the search term, and display the results if any.
     27- Create in '''templates''' file called '''search.html''' with the code:
     28{{{
     29<form action="/" method="post">
     30<input type="text" name="term" size="30"> <input type="submit" value="Search">
     31</form>
     32}}}
     33A simple form that sends the data to / (main url)
     34
     35- Edit '''searchengine/views.py''' to get:
     36{{{
     37from django.shortcuts import render_to_response
     38from django.http import Http404, HttpResponse, HttpResponseRedirect
     39
     40def search(request):
     41
     42    if request.POST:
     43        print print request.POST['term']
     44        return HttpResponseRedirect("/")
     45    else:
     46        return render_to_response('search.html')
     47}}}
     48We have a simple view which returns a template “search.html” if no POST data is available and redirects to the / main page when the POST data is available.
     49- Hook the view to the main URL by editing '''urls.py''' and entering the code:
     50
     51{{{
     52from django.conf.urls.defaults import *
     53
     54urlpatterns = patterns('',
     55(r'^/?$', 'google.searchengine.views.search'),
     56)
     57}}}
     58When we enter the main page - http://localhost:8080/ we will see the form. When we enter a term and send the form we will be redirected to the main page. The form works. Note the:
     59{{{
     60print print request.POST['term']
     61}}}
     62in the view. You should see the term from the form in the terminal with running django server. '''request.POST''' is a dictionary like object which we can easily access (key is the form field name).
     63
     64We have the term in the view but we have to do something with it – do google search. '''web_search.py''' module is easy in use, an example:
     65{{{
     66from web_search import google
     67for (name, url, desc) in google('search term', 20):
     68        print name, url
     69}}}
     70we need to use this code, pass the term form the form and then pass the search results to a template and send them to the browser. Edit our '''view.py''' to:
     71{{{
     72from django.shortcuts import render_to_response
     73from django.http import Http404, HttpResponse, HttpResponseRedirect
     74# from project.application.web_search....
     75from google.searchengine.web_search import google
     76
     77def search(request):
     78
     79    if request.POST:
     80        return render_to_response('search.html', {'result': google(request.POST['term'], 10)})
     81        #return HttpResponseRedirect("/")
     82    else:
     83        return render_to_response('search.html')
     84}}}
     85Now we don't redirect but we send the '''search.html''' template with a variable called '''result''' that has the search results. To see the results we need to modify the '''search.html''' template:
     86{{{
     87<form action="/" method="post">
     88<input type="text" name="term" size="30"> <input type="submit" value="Search">
     89</form><hr>
     90{% if result %}
     91        {% for res in result %}
     92        <li>{{ res }}</li>
     93        {% endfor %}
     94{% endif %}
     95}}}
     96The '''if result''' tag will pass if the variable exist (if we POSTed the data) the '''for''' will show all data from the search. Test it. You will see that each result is a list:
     97{{{
     98('Wine Development HQ', 'http://www.winehq.com/', 'Wine is a free implementation of Windows on Unix. WineHQ is a collection of resources for Wine developers and users.')
     99}}}
     100element 0 – page title, element 1 – page URL, element 2 – page description. To make it look as it should we edit our '''for''' loop to:
     101{{{
     102{% for res in result %}
     103        <a href="{{ res.1 }}"><b>{{ res.0 }}</b></a><br>
     104        {{ res.2 }}<br><br>
     105{% endfor %}
     106}}}
     107And it's done. Check out a '''screenshot [http://www.fotosik.pl/pokaz_obrazek/pelny/9a3a86bb255c2a82.html here]'''
     108A Polish version of this tutorial can be found on [http://www.linux.rk.edu.pl www.linux.rk.edu.pl]
Back to Top