I want to redirect users to either a ListView or DetailView based on their role through the SiteDispatchView. If DetailView, I want to pass the request.user.site
into the DetailView, but I encounter: AttributeError: Generic detail view SiteDetailView must be called with either an object pk or a slug in the URLconf.
My URL for the DetailView is path('<int:pk>/', SiteDetailView.as_view(), name='detail-site'),
(Note that site is a OnetoOneField with the Site and User models.)
# views.py
class SiteDispatchView(LoginRequiredMixin, View):
def dispatch(self, request, *args, **kwargs):
if request.user.role >= 2:
return SiteDetailView.as_view()(request, self.request.user.site)
else:
return SiteListView.as_view()(request)
class SiteDetailView(LoginRequiredMixin, generic.DetailView):
template_name = "project_site/site_detail.html"
context_object_name = "project_sites"
model = Site
def get_success_url(self):
return reverse("project_site:detail-site")
CodePudding user response:
The problem is that your SiteDetailView class needs an argument int:pk. So you must to add this argument to your return in both classes:
return reverse("project_site:detail-site", args=(request.user.site.id,))
EDIT:
As a tip, you should use reverse_lazy() instead of reverse() when working with classes.
https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse-lazy