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)
javascript ajax django
add a comment |
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)
javascript ajax django
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
add a comment |
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)
javascript ajax django
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
javascript ajax django
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
add a comment |
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
add a comment |
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.
Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago
add a comment |
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.
Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago
add a comment |
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.
Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago
add a comment |
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.
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.
answered 16 hours ago
Daniel Roseman
437k40565622
437k40565622
Thanks, it worked perfectly.
– Amandeep Singh
15 hours ago
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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