Here is the code snippet in question:
import json
class UserPageView(TemplateView):
def get(self, request, *args, **kwargs):
obj = User.objects.get(pk=17).username
return TemplateResponse(request, template="user.html", context={'userya':json.dumps(obj)})
Upon trying to access the JSON data in my template using console.log(userya);
, I encounter an error that states:
ReferenceError: userya is not defined
I am seeking guidance on how to successfully pass JSON data from Django backend to the front-end where it can be accessed by JavaScript.
Additionally, I have explored implementing this with DjangoRestFramework (DRF) but have been unsuccessful thus far. Any insights on accomplishing this task with DRF would be greatly appreciated.
It should also be noted that within my user.html
template, a link is made to a user.js
file where the console.log(userya)
statement is being executed.