0

I am new to ajax and using Django for web development. Now My Template contains : sample.html

<html>
<body>
<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(){
        var ajaxRequest;  // The variable that makes Ajax possible!

        try{
                // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
                // Internet Explorer Browsers
                try{
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try{
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e){
                                // Something went wrong
                                alert("Your browser broke!");
                                return false;
                        }
                }
        }



        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                        document.myForm.time.value = ajaxRequest.responseText;
                }
        }
        ajaxRequest.open("GET", "/showtime/", true);
        ajaxRequest.send(null);
}

</script>



<form name='myForm'>
Name: <input type='text' onBlur="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>

In views.py my function is :

def showtime(request):
       string = "Ajax Application"
       data = {"string" : string}
       pprint (data)
       return render_to_response("sample.html",data)

Now, The output is not as expected . The template does not receives the response sent by the server What is wrong with the code ?

3
  • 1
    I don't see {{ string }} anywhere in your template. What your code suposed to do? Commented Jun 7, 2010 at 11:37
  • what does the template receive? Is the time field getting populated with the full html page? Commented Jun 7, 2010 at 11:52
  • Are you sure you have the correct url pattern entry in place? Commented Jun 7, 2010 at 11:57

2 Answers 2

4

If you're trying to populate the text-field with AJAX returned string, you should not use render_to_response. Just return the string.

def showtime(request):
   s = "Ajax Application"
   print "Returning %s"%s #is this line executed?
   return HttpResponse(s, mimetype="text/plain")
Sign up to request clarification or add additional context in comments.

3 Comments

you always should return a HttpResponse object like return HttpResponse("Ajax Application", mimetype="text/plain")
again: DON'T use the print statement. in some server setups the print statement causes a HTTP 500 error (as far as I remember in wsgi or fcgi this is the case). Just return the HttpResponse.
@mawimawi I'm not suggesting he use this in production code; just trying to make sure the function indeed gets called. OP hasn't shown his urls.py yet.
0
  1. try if /showtime/ works as expected when you type in your browser!
  2. using a js framework like jquery will save you time and energy implementing ajax stuff, eg. see this tutorial http://lethain.com/entry/2007/dec/11/two-faced-django-part-5-jquery-ajax/
  3. there are also some django-built-ins that you can use, eg request.is_ajax to verify if the request is really comng from ajax!

1 Comment

is_ajax is good, but you lose the ability to just hit the URL in your browser to check the response. I only use is_ajax when I want the same view to respond differently to an ajax request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.