Reputation: 1069
Django==2.2.2
Urlpatterns:
urlpatterns = [
re_path(r'^campaigns/$', CampaignsListView.as_view(), name="campaigns_list"),
re_path(r'^campaigns/(?P<ids>\w+)/$', CampaignsDetailView.as_view(), name="campaigns_detail"),
]
My url:
http://localhost:8000/campaigns/?ids=44174865,44151214,44049374
The problem: This url leads to CampaignsListView rather than to CampaignsDetailView. How can I direct this request to CampaignsDetailView?
Upvotes: 1
Views: 24
Reputation: 476729
The ?ids=44174865,44151214,44049374
part is not part of the path of a URL, but of the querystring [wiki]. You can not direct to a different view based on the querystring. The content of the querystring is processed into request.GET
[Django-doc], a dictionary-like object.
You thus should handle this in the view itself. For example you can filter the list view given there are ids
values:
class CampaignsListView(ListView):
# ...
def get_queryset(self):
qs = super().get_queryset()
ids = request.GET.get('ids')
if ids:
try:
return qs.filter(id__in=map(int, ids.split(',')))
except ValueError:
return qs
return qs
Or you can check if the URL contains a queryset, and then let the CampaignsDetailView
do the work, like:
class CampaignsListView(ListView):
# ...
def get(self, request, *args, **kwargs):
if 'ids' in request.GET:
return CampaignsDetailView.as_view()(request, *args, **kwargs)
return super().get(request, *args, **kwargs)
Although it looks a bit "ugly".
Upvotes: 1