开发者

How do I import the render_to_response method from Django 1.1 inside of Google App Engine?

I'm a Python newbie, so I'm sure this is easy. Here's the code in my main.py:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.1')

# Use django form library.
from django import forms
from django.shortcuts import render_to_response

The la开发者_运维问答st line breaks with an ImportError. If I don't include that, then I get an error that "render_to_response" isn't available. What am I doing wrong?


Well, render_to_response is a shortcut for this, so give this a try:

from django.template import Context, loader
from django.http import HttpResponse

def render_to_response(tmpl, data):
    t = loader.get_template(tmpl)
    c = Context(data)
    return HttpResponse(t.render(c))

render_to_response("templates/index.html", {"foo": "bar"})


In this case the problem ended up being that Google App Engine uses version 0.96 of Django and it actually couldn't find the 'redirect' method, because that's only in Django 1.1. If you use the GAE utility method 'use_library' you can specify what version of the Django framework you want to use and this problem goes away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜