How to pass context value from views.py to getElementById in JavaScript?

281 views
Skip to first unread message

Mayur Bagul

unread,
May 3, 2019, 8:12:26 AM5/3/19
to Django users
Hello community,

I have stored single value from database inside a variable which is playing role of context inside views.py function.

Now I want to use this context(which holds single value) in JavaScript to change specific HTML content of template which will give me desired webpage.

I Searched on Google but I didn't found any useful tutorial or solution on specified problem.

I'm looking forward to get solution from community.

Thanking you.

sachinbg sachin

unread,
May 3, 2019, 8:43:07 AM5/3/19
to django...@googlegroups.com
Pass the id in context in Django by default every object has Id 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/37395263-cf1d-4a7d-ad51-911e68da810b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ravi Kumar

unread,
May 3, 2019, 9:03:50 AM5/3/19
to django...@googlegroups.com
Try to use ajax

Mayur Bagul

unread,
May 3, 2019, 10:41:00 AM5/3/19
to Django users
I don't want to anything in context because I already passing value of database inside it.

What I want that how I can pass that context to JavaScript getElementById().innerHTML='context'

Like this

sachinbg sachin

unread,
May 3, 2019, 10:42:43 AM5/3/19
to django...@googlegroups.com
In JavaScript I don't know buddy

sachinbg sachin

unread,
May 3, 2019, 10:43:28 AM5/3/19
to django...@googlegroups.com
If u want to use that context in html means for I context ,I.user name,i.products like that u can use

John Bagiliko

unread,
May 3, 2019, 11:33:14 AM5/3/19
to django...@googlegroups.com
Dump this result into json and render it on a page. Use AJAX to get this json data in JavaScript.

John Bagiliko

unread,
May 3, 2019, 11:35:36 AM5/3/19
to django...@googlegroups.com
import serializers from django.core
You can simply use the serializers.serialize method to do this. No need to use Restframework. 

Gurmeet Kaur

unread,
May 3, 2019, 11:45:53 AM5/3/19
to django...@googlegroups.com
I have done this in my project. We converted the object ( or context object) that you want to pass in list object in python function and then casted it with Json response object.

Something like below: 

 myqueryset_new = list(myqueryset1)
    finalmyqueryset = JsonResponse(myqueryset_new, safe=False)
    return finalmyqueryset 

and this finalmyqueryset object will come in data when you will try to call this view.py function using ajax.

Let me know if it works for you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Nader Elsisi

unread,
May 3, 2019, 11:59:42 AM5/3/19
to django...@googlegroups.com
Examples 
Reserve seats in a plane or movie. 
Manage land lots


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mayur Bagul

unread,
May 5, 2019, 1:54:21 AM5/5/19
to django...@googlegroups.com
hi gurmeet,

Thanks for help. as you given solution i didnt learned jason and ajax.
so should i learn its syntax and  to understand how i can use this inside django?
or should i learn specific portion of it.
Because when i see provide code from u guys i dint understand how to use it.

it will be better if you provide any resource link. 

Thanking you,
Mayur Bagul.

Mayur Bagul

unread,
May 5, 2019, 2:00:48 AM5/5/19
to django...@googlegroups.com
hi john,

thanks for help. 
i new to django so i dont know how its work and how its syntax  is. until i found resource and learn it.
im not getting your solution but i will search and try to learn it.

thanking you.


Gurmeet Kaur

unread,
May 6, 2019, 10:10:45 AM5/6/19
to django...@googlegroups.com
Hi Mayur,

For your question - I would suggest you to create a situation like how will you call a function from views.py using ajax from a js file for the same page?

And then you will understand the need of ajax and then will understand how you can get the data from views.py function in a js function.

Hope this helps and if you need some more info from my end, let me know.



thanks,
Gurmeet Kaur

Reply all
Reply to author
Forward
0 new messages