简体   繁体   中英

Difficulties with Django on Google App Engine

I have a Django 1.1.1 project that works fine. I'm trying to import it to Google App Engine.

I'm trying to follow these instructions.

I run it on the dev server, and I get an import error:

ImportError at /
No module named mysite.urls

This is the folder structure of mysite/ :

app.yaml
<DIR>          myapp
index.yaml
main.py
manage.py
<DIR>          media
settings.py
urls.py
__init__.py

app.yaml:

application: mysite
version: 1
runtime: python
api_version: 1

handlers:
- url: .*
  script: main.py

from settings.py :

ROOT_URLCONF = 'mysite.urls'

What am I doing wrong?

UPDATE :

now I get this error:

Request Method:     GET
Request URL:    http://localhost:8082/
Exception Type:     AttributeError
Exception Value:    'module' object has no attribute 'autodiscover'
Exception Location:     C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py in LoadModuleRestricted, line 1782

main.py :

import logging, os

# Google App Engine imports.
from google.appengine.ext.webapp import util

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
# 'project' is the name of the project created with django-admin.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import logging
import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher

def log_exception(*args, **kwds):
    logging.exception('Exception in request:')

# Log errors.
django.dispatch.dispatcher.connect(
    log_exception, django.core.signals.got_request_exception)

# Unregister the rollback event handler.
django.dispatch.dispatcher.disconnect(
    django.db._rollback_on_exception,
    django.core.signals.got_request_exception)

def main():
    # Create a Django application for WSGI.
    application = django.core.handlers.wsgi.WSGIHandler()

    # Run the WSGI CGI handler with that application.
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

Directory structure of engineapp/ :

<DIR> mysite
app.yaml
index.yaml
main.py

Directory structure of engineapp/mysite :

<DIR> myapp
<DIR> media
__init__.py
initial_data.json
manage.py
settings.py
urls.py

I feel like I'm getting closer, but still not there.

Try changing ROOT_URLCONF to just 'urls' . I don't think the parent directory of your app (in the App Engine sense, not the Django sense) directory is on sys.path when running on App Engine, which means that it doesn't see mysite as a Python package/module.

EDIT to keep up with edited question:

Now it sounds like you're inadvertently using Django 0.96 but expecting to use Django 1.1+. When you import django on App Engine, you will end up with 0.96 unless you explicitly tell App Engine you want to use a different version.

Something like

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

from jonmiddleton's answer should do the trick. Please note that in order to use this on the development server, you must have your own copy of Django 1.1 installed, because it's not bundled with the SDK.

Please also note that, as far as I know, you're not going to have any luck using the Django admin site on App Engine.

Here is my main.py:

import os
import sys
import logging

# Google App Hosting imports.
from google.appengine.ext.webapp import util
from google.appengine.dist import use_library

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
sys.path.append("/home/brox/tmp/mysite")

use_library('django', '1.1')

# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.DEBUG)

def log_exception(*args, **kwds):
  logging.exception('Exception in request:')

# Force sys.path to have our own directory first, so we can import from it.
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db

# Log errors.
django.dispatch.Signal.connect(
   django.core.signals.got_request_exception, log_exception)

# Unregister the rollback event handler.
django.dispatch.Signal.disconnect(
django.core.signals.got_request_exception,
django.db._rollback_on_exception)

def main():    
    # Create a Django application for WSGI.
    application = django.core.handlers.wsgi.WSGIHandler()

    # Run the WSGI CGI handler with that application.
    util.run_wsgi_app(application)

if __name__ == "__main__":
    main()

As you can see there are additional paths and the django error logging is a bit different...

Hope that it helps you.

I haven't used pure django with appengine. But from what I've read you need the djangoappengine-patch to make it work. You can read about it and download it from http://www.allbuttonspressed.com/projects .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM