Hello

I am trying to use a class based generic view (DetailView) to view user 
profiles.

What I am trying to achieve is: if no user PK is provided in the URL show 
the logged in user. If there is a user PK in the URL show that user. Thus 
reducing the need to have 2 views.

My code currently errors out complaining of a missing key error "pk" if I 
do not specify a PK in the URL. If a PK is supplied in the URL it works 
fine.

Any help would be appreciated.

Thank you


This is my code so far:

class home(DetailView):
    context_object_name = 'profile'
    template_name = 'view_profile.html'

    def get_context_data(self, **kwargs):
        context = super(home, self).get_context_data(**kwargs)
        u_ct = ContentType.objects.get_for_model(get_user_model()).id
        
        context.update({
            'profile_ct': u_ct,
        })
        return context

    def get_queryset(self):
        UserModel = get_user_model()

        pk = self.kwargs['pk']
        
        if not pk:
            profile = UserModel.objects.filter(pk=self.request.user)
        else:
            profile = UserModel.objects.filter(pk=self.kwargs['pk'])
        return profile

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/yXHgVvDdOaEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to