Get javascript array in django

here is my code:

urls.py:

from django.conf.urls import patterns
from views import show,showpage

urlpatterns = patterns('',
                       (r'^$',showpage),
                       (r'^show/$',show),
)

views.py

from django.http import HttpResponse
def show(request):
  arr = reqeust.GET.get('arr','Nothing')
  print arr#**it 'Nothing'**
  if arr == 'Nothing':
    msg = 'fail'
  else:
    msg = 'pass'
  return HttpResponse(msg)

def showpage(request):
  html = '''    <html>
<head>
<script language="javascript" type="text/javascript" src="/site_media/js/jquery-1.7.2.js"></script>
</head>
<body>
<input type="button" id="input_btn"/>
</body>
<script language="javascript" type="text/javascript">
$(function(){
  $("#input_btn").bind("click",function(){
    arr = [1,2,3,4,5,6]
    $.ajax({
      type:"GET",
      url:"/show/",
      data:{arr:arr},//**I want pass the arr to django**
      success:function(data){alert(data);},
      error:function(data){alert(data);}
    });
  });
})
</script>
</html>'''
  return HttpResponse(html)

when I access localhost: 8000 and click the button, I got "Nothing" printed on the backend console, since you know that I got the message "fail" in the interface

Maybe someone will handle this problem, I want to get a code ( arr = reqeust.GET.get('arr','Nothing')) that returns a list ( [1,2,3,4,5,6]) or something like this

--------- append1 -------

I got this in the console when I press btn

[12 / Jun / 2012 09:48:44] "GET / show /? Arr% 5B% 5D = 1 & arr% 5B% 5D = 2 & arr% 5B% 5D = 3 & arr% 5B% 5D = 4 & arr% 5B% 5D = 5 & arr% 5B% 5D = 6 HTTP / 1.1 "200 4

--------- apend2 -------

I fixed my code as follows:

arr = request.GET.get('arr[]','Nothing')

6, javascript, ?

!

+5
2

jQuery , , PHP. Django, . request.GET['arr[]'] request.GET.getlist('arr[]').

+10

:

$.ajaxSettings.traditional = true;

, jQuery HTTP, Django.

, request.GET request.POST (.. ajax-posting form).

django.

+5

All Articles