0

I am trying out to build full-text searching by using sphinx search, postgresql & django based on this tutorial: http://pkarl.com/articles/guide-django-full-text-search-sphinx-and-django-sp/. All setup done for sphinx & postgresql and it works but I got trouble when reach on Sample Django code part.

In django views & urlconf, I only changed the function of *search_results* into search and Story model with my own model. For URLConf, I only changed *search_results* into search just same like on views and nothing changed made on search template.

So when I try to search from my form in Django, I get exception:

TypeError at /search/
list() takes exactly 1 argument (0 given)

I also try to changed based on steyblind's comment by change the urlpattern & view definition like this:

(r'^search/(.*)?', search),

def search(request, query=''): 

but still keep get TypeError exception. Is there any mistake I am doing here? Thanks in advance.

Here's my snippets:

Urls.py

(r'^search/(.*)', search),

Views.py

def search(request, query):
    try:
        if(query == ''):
            query = request.GET['query']
        results = Flow.search.query(query)
        context = { 'flows': list(results),'query': query, 'search_meta':results._sphinx }
    except:
        context = { 'flows': list() }

    return render_to_response('search.html', context, context_instance=RequestContext(request))

search.html

{% extends "base.html" %}

{% block main %}

<div>
    <form action="/search/" method="GET">
        <input type="text" name="query"/>
        <input type="submit">
    </form>

    {% if flows %}
        <p>Your search for &ldquo;<strong>{{ query }}</strong>&rdquo; had <strong>{{ search_meta.total_found }}</strong> results.</p>
        <p>search_meta object dump: {{ search_meta }}</p>
    {% endif %}
    <hr/>
    {% for s in flows %}
            <h3><a href="{{ s.get_absolute_url }}">{{ s.title }}</a></h3>
            <p>(weight: {{ s.sphinx.weight }})</p>
            <p>story.sphinx object dump: {{ s.sphinx }}</p>
    {% empty %}
        <p>YOU HAVEN'T SEARCHED YET.</p>
    {% endfor %}
</div>

{% endblock %}
panjianom
  • 236
  • 4
  • 18

1 Answers1

1

Correct me if I'm wrong, but Django-Sphinx seems to be an abandoned project. The last update to it was a year ago, with most updates being 3-5 years ago. Also, I cannot speak for Django then, but it can do now, out of the box, what you are trying to do with Sphinx.

What version of Django and Python are you using? The error you are getting is strange as list() can take no arguments. Try this in a python shell:

>> list()
[]

Regardless, I've made a few modifications to the code that could possibly help fix the issue. However, if there are no results, you are passing 'flows' as empty in this line:

context = { 'flows': list() }

If you look at the template, this really accomplishes nothing.

urls.py:

(r'^search/', search),

views.py:

def search(request):
        query = request.GET.get('query')
        if query:
            results = Flow.search.query(query)
        if results:
            context = { 'flows': list(results),'query': query, 'search_meta':results._sphinx }
        else:
            context = { 'flows': list() }
    return render_to_response('search.html', context, context_instance=RequestContext(request))

All that said, I'd highly suggest that since this project is so outdated that you use your own search. Or if you need more functionality, you could use a search app like Haystack which is updated frequently. Using the same urls.py as above, you could implement the below as an easy search that will return all results for a blank search, the actual filtered results for a query.

views.py:

def search(request):
    query = request.GET.get('q')
    results = Flow.objects.all()
    if query:
        results = results.query(query)
    return render_to_response('search.html', {"flows": results,}, context_instance=RequestContext(request))
Dan Hoerst
  • 6,222
  • 2
  • 38
  • 51
  • I am using Django 1.4.3 & Python 2.7.3. I only try build a search function on my Django within PostgreSQL as database and free choice for the search engine. I'm also try both your codes but still no luck, it's only return an empty result nor **YOU HAVEN'T SEARCHED YET** words. May be I will try haystack, thanks for your help Dan. – panjianom Jan 27 '13 at 19:23
  • The template is incorrect as well. You will receive **YOU HAVEN'T SEARCHED YET** for every value in `flows` with where it is now. Instead you should put it between `{% if flows %}` and `{% endif %}` in an `{% else %}` statement. Does your `Flow` model have a `get_absolute_url` attribute and `title` field? – Dan Hoerst Jan 28 '13 at 01:53