Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:46:32+00:00 2026-05-13T08:46:32+00:00

As I am an impressed reader of Stack Overflow I want to ask my

  • 0

As I am an impressed reader of Stack Overflow I want to ask my first question here. Since I encountered a problem with a snippet and I do not know whether I made a mistake or it’s a bug in the code I’m using.

I adapted this code for my own site:

http://blog.tkbe.org/archive/django-admin-search-functionality/

It works fine and it’s really a great snippet.
But if my search query has length 2, I think that the results are not correct.

So for example if I search for “re” in first name and last name, I get the following results:

Mr. Tom Krem
Ms. Su Ker

Which is pretty strange. For queries with length > 2 I do not encounter this problem.
So maybe this post read somebody who is using the snippet above and can tell me whether he/she encounters the same problem.

If nobody else encounters the problem I know at least that I have a bug somewhere in my code. Maybe in the form I’m using, or something is messed up in the request context.

How can I solve this problem?

Edit 1:

The inclusion tag:

from django import template
from crm.views import SEARCH_VAR

def my_search_form(context):
    return {
        'context': context,
        'search_var': SEARCH_VAR
    }

register = template.Library()
register.inclusion_tag('custom_utilities/my_search_form.html')(my_search_form)

The my_search_form.html:

<div id="toolbar"><form 
      id="changelist-search" 
      action="" 
      method="get"> 
        <div><!-- DIV needed for valid HTML --> 
            <label 
              for="searchbar"><img src="{{ context.media_url }}/crm/img/search.png" 
              class="icon" 
              alt="Search" /></label> 
            <input 
              type="text" 
              size="40" 
              name="{{ search_var }}" 
              value="{{ context.query }}" 
              id="searchbar" /> 
            <input type="submit" value="Search" />
        </div>
    </form>
</div> 
<script 
  type="text/javascript">document.getElementById("searchbar").focus();
</script>

The view:

@login_required
def crm_contacts(request):
    query = request.GET.get('q', '')
    #pass additional params to the SortHeaders function
    #the additional params will be part of the header <a href...>
    #e.g. use it for pagination / use it to provide the query string
    additional_params_dict = {'q': query}
    foundContacts = search_contact(request,query)
    sort_headers = SortHeaders(request, LIST_HEADERS, default_order_field=1, additional_params=additional_params_dict)
    if foundContacts is not None:
        contact_list = foundContacts.order_by(sort_headers.get_order_by())
    else:
        contact_list = Contact.objects.order_by(sort_headers.get_order_by())
    context = {
        'contact_list' : contact_list,
        'headers': list(sort_headers.headers()),
        'query' : query,
    }
    return render_to_response("crm/contact_list.html", context,
                           context_instance=RequestContext(request))

The contact search form:

#models
from crm.models import Contact
from django.db.models import Q

'''
A search form from
http://blog.tkbe.org/archive/django-admin-search-functionality/
adapted to search for contacts.
'''
def search_contact(request,terms=None):
    if terms is None:
        return Contact.objects.all()
    query = Contact.objects
    for term in terms:
        query = query.filter(
            Q(first_name__icontains=term)
            | Q(last_name__icontains=term))
    return query

Another edit:

I’m using this snippet to sort the table. Probably one should know this in order to understand the code posted above.

Since I can not post links (spam protection) I will try to explain where to find it. Go to Google. Type in: django snippet table sort

Then it should be the second hit. Sort table headers. snippet nr. 308.

Edit: Add the SortHeaders() function

ORDER_VAR = 'o'
ORDER_TYPE_VAR = 'ot'

class SortHeaders:
    """
    Handles generation of an argument for the Django ORM's
    ``order_by`` method and generation of table headers which reflect
    the currently selected sort, based on defined table headers with
    matching sort criteria.

    Based in part on the Django Admin application's ``ChangeList``
    functionality.
    """
    def __init__(self, request, headers, default_order_field=None,
            default_order_type='asc', additional_params=None):
        """
        request
            The request currently being processed - the current sort
            order field and type are determined based on GET
            parameters.

        headers
            A list of two-tuples of header text and matching ordering
            criteria for use with the Django ORM's ``order_by``
            method. A criterion of ``None`` indicates that a header
            is not sortable.

        default_order_field
            The index of the header definition to be used for default
            ordering and when an invalid or non-sortable header is
            specified in GET parameters. If not specified, the index
            of the first sortable header will be used.

        default_order_type
            The default type of ordering used - must be one of
            ``'asc`` or ``'desc'``.

        additional_params:
            Query parameters which should always appear in sort links,
            specified as a dictionary mapping parameter names to
            values. For example, this might contain the current page
            number if you're sorting a paginated list of items.
        """
        if default_order_field is None:
            for i, (header, query_lookup) in enumerate(headers):
                if query_lookup is not None:
                    default_order_field = i
                    break
        if default_order_field is None:
            raise AttributeError('No default_order_field was specified and none of the header definitions given were sortable.')
        if default_order_type not in ('asc', 'desc'):
            raise AttributeError('If given, default_order_type must be one of \'asc\' or \'desc\'.')
        if additional_params is None: additional_params = {}

        self.header_defs = headers
        self.additional_params = additional_params
        self.order_field, self.order_type = default_order_field, default_order_type

        # Determine order field and order type for the current request
        params = dict(request.GET.items())
        if ORDER_VAR in params:
            try:
                new_order_field = int(params[ORDER_VAR])
                if headers[new_order_field][1] is not None:
                    self.order_field = new_order_field
            except (IndexError, ValueError):
                pass # Use the default
        if ORDER_TYPE_VAR in params and params[ORDER_TYPE_VAR] in ('asc', 'desc'):
            self.order_type = params[ORDER_TYPE_VAR]

    def headers(self):
        """
        Generates dicts containing header and sort link details for
        all defined headers.
        """
        for i, (header, order_criterion) in enumerate(self.header_defs):
            th_classes = []
            new_order_type = 'asc'
            if i == self.order_field:
                th_classes.append('sorted %sending' % self.order_type)
                new_order_type = {'asc': 'desc', 'desc': 'asc'}[self.order_type]
            yield {
                'text': header,
                'sortable': order_criterion is not None,
                'url': self.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
                'class_attr': (th_classes and ' class="%s"' % ' '.join(th_classes) or ''),
            }

    def get_query_string(self, params):
        """
        Creates a query string from the given dictionary of
        parameters, including any additonal parameters which should
        always be present.
        """
        params.update(self.additional_params)
        return '?%s' % '&amp;'.join(['%s=%s' % (param, value) \
                                     for param, value in params.items()])

    def get_order_by(self):
        """
        Creates an ordering criterion based on the current order
        field and order type, for use with the Django ORM's
        ``order_by`` method.
        """
        return '%s%s' % (
            self.order_type == 'desc' and '-' or '',
            self.header_defs[self.order_field][1],
        )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:46:37+00:00Added an answer on May 13, 2026 at 8:46 am

    If you run manage.py shell and then:

    >>> from crm.models import Contact
    >>> from django.db.models import Q
    >>> list=Contact.objects.filter(Q(first_name__icontains='re')|Q(last_name__icontains='re'))
    >>> print list
    

    What is the output?


    Edit: Right, so if you try:

    >>> list=Contact.objects.filter(Q(first_name__icontains='mot')|Q(last_name__icontains='mot'))
    >>> print list
    

    (I’m trying to narrow down on the terms that are giving you problem and I saw your last comment)

    What is the output?


    Edit: If both of the above queries work in the shell, something else is modifying your queryset somewhere and adding some additional criteria…

    Are you sure sort_headers() is not modifying the queryset with more than just an order by clause? Could you post sort_headers() to your question?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post here on stackoverflow and am very impressed by the
i saw today's logo of google and i am impressed. i want to know
One of the features that really impressed me with Stack Overflow was the simple
When I first switched from Java to C#, I was really impressed with C#'s
I'm pretty new to jQuery... so far I'm impressed. But I encountered a situation
I am just getting to know numpy, and I am impressed by its claims
I was impressed by google's MapMaker design.I would like to know what is the
Someone on here recently recommended BWToolkit, and it really impressed me, so I started
Using ActiveAdmin for a rails application (very impressed so far). My question is, when
I have used UtraEdit in the past, but was not entirely impressed for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.