在远程API中找不到Django(但在app中工作!)

时间:2017-03-21 11:08:09

标签: python django google-app-engine

见下面的来源。我使用以下部署的应用程序:

gcloud app deploy

Django模板工作正常,如加载时所见:

https://myapp.appspot.com/foo.txt

但是,当连接到远程API时,出现错误:

C:\myapp>"C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\remote_api_shell.py" myapp
App Engine remote_api shell
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]
The db, ndb, users, urlfetch, and memcache modules are imported.
s~myapp> from main import Foo
WARNING:root:You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see https://developers.google.com/appengine/docs/python/tools/libraries#Django
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "main.py", line 3, in <module>
    from google.appengine.ext.webapp import template
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\webapp\template.py", line 63, in <module>
    webapp._config_handle.django_setup()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\webapp\__init__.py", line 176, in _django_setup
    import django
ImportError: No module named django
s~myapp>

使用Python 2.5,这个问题不存在。我可以成功连接到远程API,从main.py导入类并填充数据存储区。但是,Python 2.5在GAE上是being shut down

为什么Django适用于应用程序而不适用于远程API?需要改变什么?

来源:

  • app.yaml

    runtime: python27
    api_version: 1
    threadsafe: false
    builtins:
    - remote_api: on
    handlers:
    - url: /.*
      script: main.py
    
  • foo.txt

    Host: '{{ host }}'
    
  • main.py

    import os
    import google.appengine.ext.db
    from google.appengine.ext.webapp import template
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    from google.appengine.ext import db
    
    class Foo(db.Model):
        bar = db.IntegerProperty()
    
    class FooTxt(webapp.RequestHandler):
        def get(self):
            template_values = {
                'host': self.request.host
                }
    
            path = os.path.join(os.path.dirname(__file__), 'foo.txt')
            self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
            self.response.out.write(template.render(path, template_values))
    
    application = webapp.WSGIApplication([('/foo.txt', FooTxt)], debug = False)
    
    def main():
        run_wsgi_app(application)
    
    if __name__ == "__main__":
        main()
    

0 个答案:

没有答案