Search code examples
djangodjango-urlsdjango-authenticationdjango-users

In Django, how do I write a url.py where users/self/ is the same as users/<pk>/, where <pk> is your logged in user pk?


I am trying to write a url.py where I have a simple view for users

urlpatterns = patterns( 'doors.view',
    url( r'^users/$'            , 'users_list'  , name = 'users_list'   ),
    url( r'^users/(?P<pk>\d+)/$', 'users_detail', name = 'users_detail' ),
    url( r'^users/self/$'       , # do some sort of redirect here       ),
)

The problem with the redirect is I don't know the pk of the logged in user in url.py. In view.py, I would obviously do a @login_required to be able to access users/self/.

Maybe I am doing this wrong way? What do you guys suggest I do?


Solution

  • My suggestion (not sure if it's the easiest one) would be to create a new view, where you can grab the user's pk and then call the users_detail view:

    @login_required
    def self_detail(request):
        return users_detail(request, request.user.pk)