2👍
Remember that django renders templates in serverside, for this reason you can not access to the objects, however, if you need any information from the bacenkd, maybe you can render in your template some as follow (ie using a div, you can use any element that you consider necessary)
<div id="username-{{request.user}}"></div
Django will be render your template and it’ll create a div with id username-this-is-a-logged-username
or in JS for example
var logged_user_id = '{{request.user.id}}'
in your case, if you are using ajax you can add neccessary info to the response in your view,
ie, in your view
def some_view(request):
response = {
"user": request.user.username,
"user_id": request.user.id,
}
# some view actions
return JsonResponse(response)
and now in your JS you can access to your data
$.ajax({
method : "POST",
url : "api",
data : {
email : email,
password: password
},
success: function (data) {
clearInputs();
// here you can access to the fields that you have defined in your view
console.log(data["username"]);
console.log(data["user_id"]);
},
error : function (error) {
console.log(error);
}
})
some_view
should be the view that answers to api
url
Source:stackexchange.com