Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango template with jquery: Ajax update on existing page
    primarykey
    data
    text
    <p>I have a Google App Engine that has a form. When the user clicks on the submit button, AJAX operation will be called, and the server will output something to append to the end of the very page where it comes from. How, I have a Django template, and I intend to use jquery. I have the following view:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript" src="scripts/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="scripts/scripts.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; welcome &lt;form id="SubmitForm" action="/" method="POST"&gt; &lt;input type="file" name="vsprojFiles" /&gt; &lt;br/&gt; &lt;input type="submit" id="SubmitButton"/&gt; &lt;/form&gt; &lt;div id="Testing"&gt; {{thebest}} &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's the script in scripts.js:</p> <pre><code>$(function() { $("#SubmitForm").click(submitMe); }); var submitMe = function(){ //alert('no way'); var f = $('#SubmitForm'); var action = f.attr("action"); var serializedForm = f.serialize(); $.ajax( { type: 'post', data: serializedForm, url: form_action, success: function( result ) { $('#SubmitForm').after( "&lt;div&gt;&lt;tt&gt;" + result + "&lt;/tt&gt;&lt;/div&gt;" ); } } ); }; </code></pre> <p>And here's my controller code:</p> <pre><code>from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext import db from google.appengine.ext.webapp import template from google.appengine.api.urlfetch_errors import * import cgi import wsgiref.handlers import os import sys import re import urllib from django.utils import simplejson class MainPage(webapp.RequestHandler): def get(self): path = os.path.join(os.path.dirname(__file__), 'Index.html') template_values={'thebest': 'thebest'} tmplRender =template.render(path, template_values) self.response.out.write(tmplRender) pass def Post(self): print &gt;&gt;sys.__stderr__,'me posting' result = 'grsgres' self.response.out.write(simplejson.dumps(result)) </code></pre> <p>As you can see, when the user clicks on the submitbutton, the controller method Mainpage.post will be called.</p> <p>Now I want to display the content of the 'result' variable right after the form, how can I do it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload