Pass empty slug field in urls

25 views
Skip to first unread message

Can Hicabi Tartanoglu

unread,
Jul 12, 2018, 7:22:32 AM7/12/18
to Django users
Hello.

I am trying to make this system work:

Restaurant/views.py:
class SearchRestaurantListView(ListView):
# template_name = 'restaurants/restaurantlocation_list.html'

def get_queryset(self):
print(self.kwargs)
slug = self.kwargs.get("slug")
if slug:
print("working")
queryset = RestaurantLocation.objects.filter(
Q(category__iexact=slug) | Q(category__icontains=slug)
)
else:
queryset = RestaurantLocation.objects.all()
return queryset

urls.py:
path('restaurants/<slug:slug>/', SearchRestaurantListView.as_view())

When the slug field is empty the debugger states it couldn't find 'restaurants/', the statement above should work no? Appearently not. Please help

Matthew Pava

unread,
Jul 12, 2018, 11:14:07 AM7/12/18
to django...@googlegroups.com

The error is due to not having a slug.  You’ll have to create a URL path to /restaurants/ if you want to allow an empty slug.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9671ccb3-51e6-41d0-b461-7def9dc2425f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages