前回、POSTで受け取った変数を全部取得する方法について書きました。
で、今回は、それをdjangoを使ってHTMLに出力する方法について。
サーバー側のプログラムは下のようになります。
import os
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import util
#django
from google.appengine.dist import use_library
use_library('django', '1.0')
#from django.utils import simplejson
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def post(self):
argsNames = self.request.arguments()
template_values = {'arg':''}
arg = {}
for i in argsNames:
arg[i] = self.request.get(i)
template_values['arg'] = arg
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
def main():
application = webapp.WSGIApplication(
[('/test/', MainHandler)],
debug=True
)
util.run_wsgi_app(application)
if __name__ == '__main__':
main() |
で、クライアント側はこのようになる。
| {% for key, value in arg.items %} key = {{ key }},value = {{ value }} {% endfor %} |
ポイントは、「items」というプロパティーで一回アクセスするところ。
これは、how to access dictionary element in django template?
でわかりました!!
ajaxでパラメータ確認するときに便利かも。
0 コメント:
コメントを投稿