Ad

How To Check If The Form Doesn't Contain Any Data

I have a form in my template (not Django form)

my views:

def advanced_search(request):
publish_houses = PublishHouse.objects.all()
authors = Author.objects.all()

context = {
    'publish_houses': publish_houses,
    'authors': authors,
    'Category_Choice': Category_Choice,

}

return render(request, 'books/advanced_search.html', context)

it only display the search form

and for displaying results:

def result(request):
publish_houses = PublishHouse.objects.all()
authors = Author.objects.all()
queryset_list = Book.objects.order_by('-book_date')

# Title
if 'title' in request.GET:
    title = request.GET['title']
    if title:
        queryset_list = queryset_list.filter(title__icontains=title)

# Author
if 'author' in request.GET:
    author = request.GET['author']
    if author:
        queryset_list = queryset_list.filter(author__name__iexact=author)

# Category
if 'category' in request.GET:
    category = request.GET['category']
    if category:
        queryset_list = queryset_list.filter(classification__iexact=category)

# Publish house
if 'publisher' in request.GET:
    publisher = request.GET['publisher']
    if publisher:
        queryset_list = queryset_list.filter(publish_house__name__iexact=publisher)

# Price
if 'min_price' and 'max_price' in request.GET:
    min_price = request.GET['min_price']
    max_price = request.GET['max_price']
    if min_price and max_price:
        queryset_list = queryset_list.filter(price__gte=min_price, price__lte=max_price)

context = {
    'publish_houses': publish_houses,
    'authors': authors,
    'Category_Choice': Category_Choice,
    'books': queryset_list,
    'values': request.GET,
}

return render(request, 'books/result.html', context)

and i want to check if there's no data in all fields to redirect back to advanced search form if there's no data

i tried to put field's data in a variable like:

    title = request.GET['title']

then check by if not for every field, but it gave me MultiValueDictKeyError

any ideas how can i do this?

thanks in advance

Ad

Answer

Use the get method providing a default value to prevent the error to be raised.

def result(request):
    publish_houses = PublishHouse.objects.all()
    authors = Author.objects.all()
    queryset_list = Book.objects.order_by('-book_date')

    keys = ['title' , 'author', 'category', 'publisher', ('min price', 'max price')]
    argument_mapper = {
        'title': 'title__icontains',
        'author': 'author__name__iexact',
        'category': 'classification__iexact',
        'publisher': 'publish_house__name__iexact',
        ('min price', 'max price'): ('price__gte', 'price__lte'), # leave the comma so this is extendable
    }
    for field in keys:
        arg = argument_mapper.get(field)
        if isinstance(arg, str):
            val = request.GET.get(arg, None)
            if val:
                queryset_list = queryset_list.filter(**{arg:val})
        elif isinstance(arg, tuple):
            vals = [request.GET.get(a, None) for a in arg]
            if all(vals):
                queryset_list = queryset_list.filter(**dict(zip(arg, vals)))

    context = {
        'publish_houses': publish_houses,
        'authors': authors,
        'Category_Choice': Category_Choice,
        'books': queryset_list,
        'values': request.GET,
    }

    return render(request, 'books/result.html', context)
Ad
source: stackoverflow.com
Ad