A Django search form that I cannot make it work
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=que开发者_运维技巧ry) |
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 %}
Could it be because you are searching by company, address and postcode, and not by client name?
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.
精彩评论