Ptar
Ptar

Reputation: 374

Get messages from django query

def search(request):
    query=request.GET.get('q')
    if query:
        queryset = (Q(adm__exact=query))
        result = Mymodel.objects.filter(queryset).distinct()
    return render(request, 'h.html',{'result':result}

I'd like to have message get back to me incase what is in the query is not available in the database. How/where do I insert the code??

Upvotes: 0

Views: 149

Answers (1)

tobias
tobias

Reputation: 291

You want something like the following:

def search(request):
    query=request.GET.get('q')
    if query:
        q = Q(adm__exact=query)
        queryset = Mymodel.objects.filter(q).distinct()
        if queryset.exists():
            # If we are here, there are results.
            return render(request, 'h.html', {'result': queryset})
    return render(request, 'h.html', {'message': 'No query or not results.'})

Upvotes: 2

Related Questions