Search Box on any template
Views
from django.db.models import Q
def search(request):
template = 'pages/search.html'
context = {}
if request.method == 'GET':
print('Get')
return render(request, template, context)
if request.method == 'POST':
query = request.POST['keyword']
category = request.POST['category']
if not query:
return render(request, template, context)
if category == 'Customers' or category == 'Any':
context['customers'] = Customer.objects.filter(Q(profile__first_name__contains=query) | Q(profile__last_name__contains=query) | Q(profile__email__contains=query)).distinct()
if category == 'Translators' or category == 'Any':
context['translators'] = Translator.objects.filter(Q(profile__first_name__contains=query) | Q(profile__last_name__contains=query) | Q(profile__email__contains=query)).distinct()
if category == 'Customers' or category == 'Any':
context['pms'] = ProjectManager.objects.filter(Q(profile__first_name__contains=query) | Q(profile__last_name__contains=query) | Q(profile__email__contains=query)).distinct()
if category == 'Requests' or category == 'Any':
context['requests'] = Request.objects.filter(Q(customer__profile__first_name__contains=query) | Q(name__contains=query) | Q(delivery_date__contains=query) | Q(customer__profile__last_name__contains=query) | Q(customer__profile__email__contains=query)).distinct()
if category == 'Quotes' or category == 'Any':
context['quotes'] = Quote.objects.filter(Q(request__customer__profile__first_name__contains=query) | Q(date__contains=query) | Q(quoteprice__price__contains=query) | Q(request__customer__profile__last_name__contains=query) | Q(request__customer__profile__email__contains=query)).distinct()
if category == 'Orders' or category == 'Any':
context['orders'] = Order.objects.filter(Q(quote_price__quote__request__customer__profile__first_name__contains=query) | Q(created_at__contains=query) | Q(quote_price__price__contains=query) | Q(quote_price__quote__request__customer__profile__last_name__contains=query) | Q(quote_price__quote__request__customer__profile__email__contains=query)).distinct()
if category == 'Projects' or category == 'Any':
context['projects'] = Project.objects.filter(Q(customer__profile__first_name__contains=query) | Q(customer__profile__last_name__contains=query) | Q(customer__profile__email__contains=query) | Q(name__contains=query) | Q(create_date__contains=query))
if category == 'Invoices' or category == 'Any':
context['invoices'] = Invoice.objects.filter(Q(order__quote_price__quote__request__customer__profile__first_name__contains=query) | Q(due_date__contains=query) | Q(paid_amount__contains=query) | Q(order__quote_price__quote__request__customer__profile__last_name__contains=query) | Q(order__quote_price__quote__request__customer__profile__email__contains=query)).distinct()
if category == 'Bills' or category == 'Any':
context['bills'] = TranslatorInvoice.objects.filter(Q(translator_order__translator__profile__first_name__contains=query) | Q(paid_amount__contains=query) | Q(translator_order__translator__profile__last_name__contains=query) | Q(translator_order__translator__profile__email__contains=query)).distinct()
context['query'] = query
context['category'] = category
return render(request, template, context)