views:

40

answers:

2

Hello I have been recently working on Django search forms recently and have tired to edit one myself. Here is a search form that is supposed to find Clients. However When I type in a clients name, it does not display that client's name. So I am wondering What I am doing wrong.

    #model.py
    class Client(models.Model):
        company = models.CharField(max_length=80)
        first_name = models.CharField(max_length=80, blank=True, null=True)
        last_name = models.CharField(max_length=80, blank=True, null=True)
        address = models.CharField(max_length=250)
        city = models.CharField(max_length=100)
        country = models.CharField(max_length=120)
        postcode = models.CharField(max_length=7)
        telephone = models.CharField(max_length=20)
        email = models.EmailField()
        additional_info = models.TextField(blank=True, null=True)

        def __unicode__(self):
                return self.company
#views.py
@login_required
def search_client(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
        Q(company__icontains=query) |
        Q(address__icontains=query) |
        Q(postcode__icontains=query)
        )
        results = Client.objects.filter(qset).distinct()
    else:
        results = []
    return render_to_response("search_clients.html", {
        "results": results,
        "query": query
}, context_instance=RequestContext(request))


    #search_clients
{% extends "base.html" %}  

    {% block content %}
    <h1>Search</h1>
      <form action="." method="GET">
        <label for="q">Search: </label>
        <input type="text" name="q" value="{{ query|escape }}">
        <input type="submit" value="Search">
      </form>

      {% if query %}
        <h2>Results for "{{ query|escape }}":</h2>

        {% if results %}
          <ul>
          {% for clients in results %}
            <li>{{ clients|escape }}</l1>
          {% endfor %}
          </ul>
        {% else %}
          <p>No clients found</p>
        {% endif %}
      {% endif %}
    {% endblock %}
+1  A: 

Could it be because you are searching by company, address and postcode, and not by client name?

cheshire
I don't think so. When I mean by clients name, I am talking about the clients company. I am sort doing something like this this http://www.djangobook.com/en/1.0/chapter07/
Shehzad009
the code looks OK to me. Try putting "print" statements everywhere to see what's actually going on and the correct view is called etc. Then try just manually searching in "./manage.py shell" like Client.objects.filter(company__icontains=<your_query_string>) to make sure the result is actually in the database
cheshire
Well I tried manually searching in "./manage.py shell" like you just said and its works fine. Client.objects.filter(company__icontains="currys"), returns [<Client: currys>]
Shehzad009
But that's not the same query you're running in the code above. Try doing that.
Daniel Roseman
A: 

Ok looks like somehow it know works properly. The odd thing is I don't know how it started to working properly. I may have restarted the server again while making changes or may have been my urls.py file. I really don't know but seems ok now.

Shehzad009