How to use current logged in user as PK for Django DetailView?

When defining URL patterns, I have to use a regex to get PK from the URL.

What if I need a URL that does not have a PC, and if it is not provided, it will use the current user? Examples:

  • Visiting /user will receive a detailed view of the current user
  • /user/edit display an UpdateView for the currently logged in user

I tried hard-coding pk= in a call to Detail.as_view() , but it reports an invalid keyword.

How to indicate that in URL conf?

My sample code that shows a PK error when visiting the /user URL:

 urlpatterns = patterns('', url(r'user/$', DetailView.as_view( model=Account, template_name='user/detail.html')), )` 
+4
source share
4 answers

An alternative approach would be to override the get_object method of the get_object subclass, something along the line:

 class CurrentUserDetailView(UserDetailView): def get_object(self): return self.request.user 

Significantly cleaner, simpler, and more class-based representations than the mixin method.

EDIT. To clarify, I believe that two different URL patterns (i.e. one with pk and the other without) should be defined separately in urlconf. Therefore, they can be served by two different types, especially since this makes the code cleaner. In this case, urlconf might look something like this:

 urlpatterns = patterns('', url(r"^users/(?P<pk>\d+)/$", UserDetailView.as_view(), name="user_detail"), url(r"^users/current/$", CurrentUserDetailView.as_view(), name="current_user_detail"), url(r"^users/$", UserListView.as_view(), name="user_list"), ) 

And I updated my example above to note that it inherits from UserDetailView , which makes it even cleaner and makes it clear that this is in fact: a special case of the parent view.

+4
source

As far as I know, you cannot define this URL definition because you do not have access to this information.

However, what you can do is create your own mixin and use it to create views that behave the way you want.

Your mixin will look something like this:

 class CurrentUserMixin(object): model = Account def get_object(self, *args, **kwargs): try: obj = super(CurrentUserMixin, self).get_object(*args, **kwargs) except AttributeError: # SingleObjectMixin throws an AttributeError when no pk or slug # is present on the url. In those cases, we use the current user obj = self.request.user.account return obj 

and then create your custom views:

 class UserDetailView(CurrentUserMixin, DetailView): pass class UserUpdateView(CurrentUserMixin, UpdateView): pass 
+2
source

General views always use RequestContext . And this point in the Django documentation says that when using RequestContext with an auth application, the template receives the passed variable user , which represents the current user logged in. So go ahead and feel free to refer to the user in your templates.

0
source

You can get information about the current user from the request object . If you want to see different user data, you can pass the URL as a parameter. The URL will be encoded as follows:

 url(r'user/(?P<user_id>.*)$', 'views.user_details', name='user-details'), 

views.user_details second parameter will be user_id , which is a string (you can change the regular expression in the URL to restrict integer values, but the parameter will still contain a type string). Here is a list of other examples of url patterns from the Django documentation.

0
source

All Articles