I have a model like this:
class Info(models.Model):
tape_id = models.TextField()
name = models.TextField()
video_type = models.TextField()
date = models.DateTimeField()
director = models.CharField(max_length=255)
cameraman = models.CharField(max_length=255)
editor = models.CharField(max_length=255)
time_code = models.TextField()
tag1 = models.TextField()
User can search from tape_id, name, director and cameraman using the same search input box.
I have a search view like this:
if request.method == 'POST':
search_inp = request.POST['search_box']
tape_id = Info.objects.filter(tape_id__exact=search_inp)
res = Info.objects.filter(name__icontains=search_inp)
res = Info.objects.filter(director__icontains=search_inp)
res = Info.objects.filter(cameraman__icontains=search_inp)
total_video = res.count()
if len(res) == 0 and len(tape_id) == 0 :
result = "No videos found!!"
return render_to_response('no_results_only.html', {'result':result}, context_instance=RequestContext(request))
else:
date1 = [i.date for i in res]
date = [i.strftime("%B %d, %Y") for i in date1]
a = zip(res, date)
return render_to_response('list_videos.html', {'a':a, 'total_video':total_video}, context_instance=RequestContext(request))
return HttpResponseRedirect('/')
I thought it would work at first but it doesn't. First res variable can contain the value whereas the last res be empty which will return to the no_results.html. I want to deploy this search using the same input box. How can I make this work?
reswith each of these queries, right? Anyway, useQforORqueries.