Pass data from html to views.

144 views
Skip to first unread message

voss

unread,
May 11, 2012, 5:37:08 PM5/11/12
to django...@googlegroups.com
Hello,

I have a list that is created dynamically, and the code looks like:

<script type="text/javascript">
    ...
    ...
    for (i=0; i<array.length; i++)
         {
              dojo.create("li", {innerHTML: array[i]}, dojo.byId("test"));
         {
</script>

<body>
    <ul id="test"></ul>
</body>

How to pass the list values (i.e.,array[]) to views for further work?

Thanks! 

Nikolas Stevenson-Molnar

unread,
May 11, 2012, 6:31:49 PM5/11/12
to django...@googlegroups.com
How about creating hidden input nodes inside a form for each li node you
create? For example, add after your dojo.create:

dojo.create("input", {name: "values[]", type: "hidden", value:
array[i]}, dojo.byId("myForm"));

_Nik
> Thanks! --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/IHfsfNwhhR8J.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

doniyor

unread,
May 12, 2012, 1:16:45 AM5/12/12
to django...@googlegroups.com
just after your for loop, you can call ajax function which takes the values of your newly created lists and submits this further to your view. you dont need to load the whole page and the data will silently go to your view. 

voss

unread,
May 14, 2012, 9:00:47 AM5/14/12
to django...@googlegroups.com
Thanks, Nik. The list is actually a result of dnd and dojo.cookie. For some reason, creating hidden input nodes does not work in this case.
> django-users+unsubscribe@googlegroups.com.

voss

unread,
May 14, 2012, 9:19:06 AM5/14/12
to django...@googlegroups.com
Hi Am, this sounds like a good idea, and I think it may work in my case (with dajax). Thank you!

I have a question, though. I know that with dajax, the data is passed to the ajax views in ajax.py, and the result is then sent back to the same html page. But how exactly does your method work? (I have no experience in ajax.)
For example, how do you call an ajax function and how do you link it to views.py?

doniyor

unread,
May 15, 2012, 7:37:12 AM5/15/12
to django...@googlegroups.com
ajax is not that different from dajax.. actually almost same logic.. but look, i try to explain how it should work.. 

your index.html

<a href="#" onclick="ajaxfunction()" ><input name="wanted_value" type="hidden"/>click me</a>

and your ajaxfunction in the head of this index.html is this. 

function ajaxfunction(){
wanted_value = $("input[name='wanted_value']").val();
$.ajax({
url: "/ajax/",
type: "POST",
data: {value_key: wanted_value}
}).complete(function(){
alert('ajax worked yihuu!');
});
}

and what it does is: it sends the data to /ajax/ url, and you catch it in your urls and forward it to your views.. then process the data and you are lucky.. 

voss

unread,
May 15, 2012, 8:59:25 AM5/15/12
to django...@googlegroups.com
Hi Am, thank you so much for the explanation. It is very helpful! And thank you for the idea of using ajax for my problem. I used dajax and it is working nicely!
Reply all
Reply to author
Forward
0 new messages