Using value of an attribute of django object in Javascript function









up vote
0
down vote

favorite












I have a model named VisitorInfo in models.py which is as follows:



class VisitorInfo(models.Model):
#To maintain clarity i am not showing all the fields.

visitor_id = models.CharField(max_length=120)
pages_visited = models.ManyToManyField(Page,blank=True)
time_zone = models.CharField(max_length=120, null= True, blank=True)
ip = models.CharField(max_length=120, null= True, blank=True)
device_type = models.CharField(max_length=120, null=True)


Following is the view where i am using this model.



class Live_Now(LoginRequiredMixin, TemplateView):
template_name = 'dashboard/live_now.html'

def get_context_data(self, **kwargs):
context = super(Live_Now, self).get_context_data(**kwargs)
search = self.request.GET.get('q')

if search:
if VisitorInfo.objects.filter(Q_filter).filter(active=True).count() >0:
context['list_no'] = VisitorInfo.objects.filter(Q_filter).filter(active=True)
else:
context['list_no'] = "N"
return context


What i need to do as soon as live_now.html page loads and Live_Now view runs, a new tab will open with following url.



url = 54.161.109.227:5000/visitor_id 


I know that to open a new tab i have to write down script in live_now.html which is as follows.



<script type='text/javascript>
window.open(url); // Above written url
</script>


But how do i fetch visitor_id of the visitor at runtime. Fetching visitor_id is the task fo python function and opening new tab is the function of javascript. How do i pass value of python variable in javascript program ?



For time being i am fetching visitor_id of first visitor in visitors and i have written the following function to do this.



def myfunc():
visitors= VisitorInfo.objects.all()
for visitor in visitors[:1]:
visitorId = visitor.visitor_id
return str(visitorId)









share|improve this question























  • on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
    – Exprator
    17 hours ago










  • PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
    – art06
    17 hours ago














up vote
0
down vote

favorite












I have a model named VisitorInfo in models.py which is as follows:



class VisitorInfo(models.Model):
#To maintain clarity i am not showing all the fields.

visitor_id = models.CharField(max_length=120)
pages_visited = models.ManyToManyField(Page,blank=True)
time_zone = models.CharField(max_length=120, null= True, blank=True)
ip = models.CharField(max_length=120, null= True, blank=True)
device_type = models.CharField(max_length=120, null=True)


Following is the view where i am using this model.



class Live_Now(LoginRequiredMixin, TemplateView):
template_name = 'dashboard/live_now.html'

def get_context_data(self, **kwargs):
context = super(Live_Now, self).get_context_data(**kwargs)
search = self.request.GET.get('q')

if search:
if VisitorInfo.objects.filter(Q_filter).filter(active=True).count() >0:
context['list_no'] = VisitorInfo.objects.filter(Q_filter).filter(active=True)
else:
context['list_no'] = "N"
return context


What i need to do as soon as live_now.html page loads and Live_Now view runs, a new tab will open with following url.



url = 54.161.109.227:5000/visitor_id 


I know that to open a new tab i have to write down script in live_now.html which is as follows.



<script type='text/javascript>
window.open(url); // Above written url
</script>


But how do i fetch visitor_id of the visitor at runtime. Fetching visitor_id is the task fo python function and opening new tab is the function of javascript. How do i pass value of python variable in javascript program ?



For time being i am fetching visitor_id of first visitor in visitors and i have written the following function to do this.



def myfunc():
visitors= VisitorInfo.objects.all()
for visitor in visitors[:1]:
visitorId = visitor.visitor_id
return str(visitorId)









share|improve this question























  • on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
    – Exprator
    17 hours ago










  • PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
    – art06
    17 hours ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a model named VisitorInfo in models.py which is as follows:



class VisitorInfo(models.Model):
#To maintain clarity i am not showing all the fields.

visitor_id = models.CharField(max_length=120)
pages_visited = models.ManyToManyField(Page,blank=True)
time_zone = models.CharField(max_length=120, null= True, blank=True)
ip = models.CharField(max_length=120, null= True, blank=True)
device_type = models.CharField(max_length=120, null=True)


Following is the view where i am using this model.



class Live_Now(LoginRequiredMixin, TemplateView):
template_name = 'dashboard/live_now.html'

def get_context_data(self, **kwargs):
context = super(Live_Now, self).get_context_data(**kwargs)
search = self.request.GET.get('q')

if search:
if VisitorInfo.objects.filter(Q_filter).filter(active=True).count() >0:
context['list_no'] = VisitorInfo.objects.filter(Q_filter).filter(active=True)
else:
context['list_no'] = "N"
return context


What i need to do as soon as live_now.html page loads and Live_Now view runs, a new tab will open with following url.



url = 54.161.109.227:5000/visitor_id 


I know that to open a new tab i have to write down script in live_now.html which is as follows.



<script type='text/javascript>
window.open(url); // Above written url
</script>


But how do i fetch visitor_id of the visitor at runtime. Fetching visitor_id is the task fo python function and opening new tab is the function of javascript. How do i pass value of python variable in javascript program ?



For time being i am fetching visitor_id of first visitor in visitors and i have written the following function to do this.



def myfunc():
visitors= VisitorInfo.objects.all()
for visitor in visitors[:1]:
visitorId = visitor.visitor_id
return str(visitorId)









share|improve this question















I have a model named VisitorInfo in models.py which is as follows:



class VisitorInfo(models.Model):
#To maintain clarity i am not showing all the fields.

visitor_id = models.CharField(max_length=120)
pages_visited = models.ManyToManyField(Page,blank=True)
time_zone = models.CharField(max_length=120, null= True, blank=True)
ip = models.CharField(max_length=120, null= True, blank=True)
device_type = models.CharField(max_length=120, null=True)


Following is the view where i am using this model.



class Live_Now(LoginRequiredMixin, TemplateView):
template_name = 'dashboard/live_now.html'

def get_context_data(self, **kwargs):
context = super(Live_Now, self).get_context_data(**kwargs)
search = self.request.GET.get('q')

if search:
if VisitorInfo.objects.filter(Q_filter).filter(active=True).count() >0:
context['list_no'] = VisitorInfo.objects.filter(Q_filter).filter(active=True)
else:
context['list_no'] = "N"
return context


What i need to do as soon as live_now.html page loads and Live_Now view runs, a new tab will open with following url.



url = 54.161.109.227:5000/visitor_id 


I know that to open a new tab i have to write down script in live_now.html which is as follows.



<script type='text/javascript>
window.open(url); // Above written url
</script>


But how do i fetch visitor_id of the visitor at runtime. Fetching visitor_id is the task fo python function and opening new tab is the function of javascript. How do i pass value of python variable in javascript program ?



For time being i am fetching visitor_id of first visitor in visitors and i have written the following function to do this.



def myfunc():
visitors= VisitorInfo.objects.all()
for visitor in visitors[:1]:
visitorId = visitor.visitor_id
return str(visitorId)






javascript ajax django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago

























asked 17 hours ago









Amandeep Singh

127




127











  • on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
    – Exprator
    17 hours ago










  • PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
    – art06
    17 hours ago
















  • on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
    – Exprator
    17 hours ago










  • PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
    – art06
    17 hours ago















on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
– Exprator
17 hours ago




on document.load function call an ajax which will call the above written function to return the visitor id and the call the window.open function to call the url
– Exprator
17 hours ago












PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
– art06
17 hours ago




PS: Most of the modern browsers prevent opening a new window/popup and gives the user the freedom to decide. You might need to use a CSS based light-box like model windows.
– art06
17 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You're making this much harder than it needs to be. If you need your JS to have a particular value, then pass it there.



def get_context_data(self, **kwargs):
... existing code ...
context['visitor_id'] = Visitor.objects.first().user_id
return context


...



url = "54.161.109.227:5000/ visitor_id ";
window.open(url)


Of course you can just as easily pass the whole Visitor object to the template and do visitor.user_id , it's up to you.






share|improve this answer




















  • Thanks, it worked perfectly.
    – Amandeep Singh
    15 hours ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224067%2fusing-value-of-an-attribute-of-django-object-in-javascript-function%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You're making this much harder than it needs to be. If you need your JS to have a particular value, then pass it there.



def get_context_data(self, **kwargs):
... existing code ...
context['visitor_id'] = Visitor.objects.first().user_id
return context


...



url = "54.161.109.227:5000/ visitor_id ";
window.open(url)


Of course you can just as easily pass the whole Visitor object to the template and do visitor.user_id , it's up to you.






share|improve this answer




















  • Thanks, it worked perfectly.
    – Amandeep Singh
    15 hours ago














up vote
0
down vote



accepted










You're making this much harder than it needs to be. If you need your JS to have a particular value, then pass it there.



def get_context_data(self, **kwargs):
... existing code ...
context['visitor_id'] = Visitor.objects.first().user_id
return context


...



url = "54.161.109.227:5000/ visitor_id ";
window.open(url)


Of course you can just as easily pass the whole Visitor object to the template and do visitor.user_id , it's up to you.






share|improve this answer




















  • Thanks, it worked perfectly.
    – Amandeep Singh
    15 hours ago












up vote
0
down vote



accepted







up vote
0
down vote



accepted






You're making this much harder than it needs to be. If you need your JS to have a particular value, then pass it there.



def get_context_data(self, **kwargs):
... existing code ...
context['visitor_id'] = Visitor.objects.first().user_id
return context


...



url = "54.161.109.227:5000/ visitor_id ";
window.open(url)


Of course you can just as easily pass the whole Visitor object to the template and do visitor.user_id , it's up to you.






share|improve this answer












You're making this much harder than it needs to be. If you need your JS to have a particular value, then pass it there.



def get_context_data(self, **kwargs):
... existing code ...
context['visitor_id'] = Visitor.objects.first().user_id
return context


...



url = "54.161.109.227:5000/ visitor_id ";
window.open(url)


Of course you can just as easily pass the whole Visitor object to the template and do visitor.user_id , it's up to you.







share|improve this answer












share|improve this answer



share|improve this answer










answered 16 hours ago









Daniel Roseman

437k40565622




437k40565622











  • Thanks, it worked perfectly.
    – Amandeep Singh
    15 hours ago
















  • Thanks, it worked perfectly.
    – Amandeep Singh
    15 hours ago















Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago




Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224067%2fusing-value-of-an-attribute-of-django-object-in-javascript-function%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo