Hi,

Code looks fine to me, maybe you could give us the exact error message ?
You could also try request.user.id or something to get the actual pk and not 
only the user object  but that’s just a guess.

Von: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Im 
Auftrag von David
Gesendet: Mittwoch, 21. November 2012 16:45
An: django-users@googlegroups.com
Betreff: DetailView

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<mailto:django-users@googlegroups.com>.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



. . . . . . . . . . . . . . . . . . . . . . . . . .

Ing. Patrick Szabo

Developer

LexisNexis

A-1030 Wien, Marxergasse 25



patrick.sz...@lexisnexis.at<mailto:patrick.sz...@lexisnexis.at>

Tel.: +43 1 53452 1573

Fax.: +43 1 534 52 146

. . . . . . . . . . . . . . . . . . . . . . . . . .


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
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