I'm new to Django, and as a learning exercise I've been putting
together a simple contact database. There are separate fields for
first_name and last_name, and another field for the company name
called ac_name
In the template for the main page I put a form that allows searching
by name or company, here's my code:
<form action="/contacts/search/" method="post">
<input type="text" name="search_parameter" id="search_parameter"><br /
>
<input type="radio" name="choice" id="choice1" value="1" checked>
<label for="choice1"">by Name</label>
<input type="radio" name="choice" id="choice2" value="2">
<label for="choice2"">by Company</label><br />
<input type="submit" value="Go" />
</form>
And here is the relevant section of the views.py file:
def search(request):
find_me = request.POST['search_parameter']
sort_by_choice = request.POST['choice']
if sort_by_choice == '1':
all_contacts =
Contact.objects.filter(last_name__icontains=find_me)
elif sort_by_choice == '2':
all_contacts =
Contact.objects.filter(ac_name__icontains=find_me)
return render_to_response('contactsdb/index.html', {'all_contacts':
all_contacts})
This currently works quite nicely as long as you only enter someone's
last name or company name, but if someone enters a person's full name
in the search box, it obviously returns nothing. I'm having trouble
figuring out how to search a person's full name for matching text. It
might be useful to know that the Contact model returns a person's full
name in the format "last_name, first_name" in the __str__() method.
Thanks for any help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---