views:

206

answers:

2

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 last 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?

+3  A: 

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"})
Jed Smith
that does work, however I'd rather not re-define the function in my file. any ideas why my import statement doesn't work? also, it complains that it can't find my template when i specify it as the full OS path (e.g. /www/controller/../templates/template.html), which is how I previously reference the template. any thoughts?
Bialecki
No, since I'm unfamiliar with Google App Engine. I've read that its Django implementation is gimped in several ways. If you'd rather not redefine the function in your file, put it in your miscellaneous or something and just import it (as you would be from `django.shortcuts` anyway, what do you think `django.shortcuts` is?)
Jed Smith
A: 

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.

Bialecki