Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources for learning Django + AJAX [closed]

I am a seasoned developer with C#, JAVA, and C/C++ experience, but mostly worked on non-web apps/processes.

I have picked up Python and Django in the last couple months for my own project. I am at the stage of needing some AJAX elements for my web app. I know only the very basic of JavaScript, let alone AJAX.

Please recommend some resources for me to learn how to use AJAX with Django, let it books and/or online materials. Note that my plan is to use JQuery as my javascript library. Thanks.

like image 431
tamakisquare Avatar asked Nov 13 '22 23:11

tamakisquare


1 Answers

<3 AJAX & Django! Very fun. Dajax tries to make working with ajax easier (although it's pretty easy to begin with). Here are a couple more blog posts:

  • http://webcloud.se/log/AJAX-in-Django-with-jQuery/
  • http://chronosbox.org/blog/jsonresponse-in-django?lang=en

And, here is a simple example you can play with (use in urls.py):

import json      
from django.http import HttpResponse
from django.template import Template, Context

def ajax(request):
    """returns json response"""
    return HttpResponse(json.dumps({'foo': 'bar'}), mimetype='application/json')

def index(request):
    """simple index page which uses jquery to make a single get request to /ajax, alerting the value of foo"""
    t = Template("""
    <!doctype html>
      <head>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
       <script type="text/javascript">
         $.get('/ajax/', function(data) {
           alert(data['foo']);
         });
       </script>
     </head>
    </html>""")
    return HttpResponse(t.render(Context()))

# urlconf
urlpatterns = patterns('',
    (r'^$', index),
    (r'^ajax/', ajax),
)
like image 72
zeekay Avatar answered Dec 25 '22 21:12

zeekay