Reputation: 7766
I am using backbone.js with django and i would like to right after the initial request to my application /
get the current logged in user.
I do have a UserResource
set up so i can make an api call api/v1/user/id
but that means i have to know the id
i am looking for which i dont. In traditional django request views, i would have the request.user
object. Is this also available to me on the client side and if not how can i make a call to get request.user
?
Edit:
Since i am using traditional authentication with Django and after authorizing the user, backbone picks up from there. Can i in my base.html file, create a variable that stores the unique id of the current logged in user like so:
var user = {{user}}; // which shows the username
Are there any risks to this
Upvotes: 0
Views: 2812
Reputation: 1854
The following would place the user id in the meta of the response:
def alter_list_data_to_serialize(self, request, data_dict):
if isinstance(data_dict, dict):
if 'meta' in data_dict:
if request.user and request.user.pk:
data_dict['meta']['user_id'] = request.user.pk
return data_dict
Personally, I've never had a need to do this. I place the user object into page context server side. Even for a single page RESTful application I would make standard request/response (not AJAX) on login, and populate the content/context server side.
Any subsequent AJAX requests should not include the user id in the same way that the suer id should not be passed in the URL. You should identify the user server-side and filter accordingly. Django makes this very easy, exposing the user using the AuthenticationMiddleware.
Upvotes: 0
Reputation: 1854
Take a look at the 'Creating per-user resources' example in the TastyPie Cookbook
http://django-tastypie.readthedocs.org/en/latest/cookbook.html#creating-per-user-resources
However, the docs are out-of-date, and the API has changed. The method signatures have changed, and the request is now an attribute of the bundle.
def authorized_read_list(self, object_list, bundle):
You can access the user from bundle.request.user
Also see https://github.com/toastdriven/django-tastypie/issues/809
Upvotes: 6