my search query works fine when i fill the book and chapter in my searchfield. but when i only fill in the book i get an error. Can somebody help me?
Request Method: GET Request URL: http://127.0.0.1:8000/search/?q=genesis Django Version: 3.0 Exception Type: ValueError Exception Value:
not enough values to unpack (expected 2, got 1)
def get_queryset(self):
query = self.request.GET.get('q')
book, chapter = query.split()
object_list = Verse.objects.filter(
Q(book__icontains=book) & Q(
chapter__exact=chapter))
return object_list
This does not work, since then there is only one word, and hence query.split()
will return a singleton list.
def get_queryset(self):
query = self.request.GET.get('q')
object_list = Verse.objects.all()
if query:
query = query.split()
object_list = object_list.filter(book__icontains=query[0])
if :
object_list = object_list.filter(chapter=query[1])
return object_list
I'm however not convinced that splitting by a space is a good idea here. If the book name contains spaces, then .split()
will thus split the title, and you will take the second word as chapter
. You might want to split on an identifier like a colon ( :
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.