Search code examples
djangodjango-1.7django-generic-views

troubles with dynamic filtering in generic-display Django


I am trying to display a list of the users. once you select the user you can view the userprofile. the thing is: UserProfile does not work. how to make it works?

#views.py
    class UserList(ListView):
        model = Userx
        template_name ='userList.html'




class UserProfile(ListView):
    template_name = 'userprofile.html'

        def get_context_data(self, **kwargs):
            self.user= get_object_or_404(Userx, name=self.args[0])
             return Userx.objects.filter(user=self.user)
 #urls.py            

            url(r'^userprofile/(?P<id>\d+)/$', UserProfile.as_view(), name='userprofile'),

Solution

  • To show the single instance you should use the DetailView instead of the ListView:

    from django.views.generic.detail import DetailView
    
    class UserProfile(DetailView):
        model = Userx
    

    And change the regex group name to pk:

    url(r'^userprofile/(?P<pk>\d+)/$', UserProfile.as_view(), name='userprofile')