2014-09-01 88 views
-1

我正在閱讀'Head First Python'。我正在介紹Google App Engine的第10章。最初使用Python和Google App Engine的hello世界很成功,但後來的程序都失敗了。Google App Engine - Django - Python - Ubuntu - 問題

我有以下的app.yaml文件:

application: three 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: false 

handlers: 
- url: /.* 
    script: page368b.py 

libraries: 
- name: django 
    version: "1.3" 

隨着下面的Python代碼(page368b.py):

import wsgiref.handlers 

from google.appengine.ext import webapp 
from google.appengine.ext import db 
from google.appengine.ext.webapp import template 

#this line throws the error when accessing the web-page 
from google.appengine.ext.db import djangoforms 

import birthDB 

class BirthDetailsForm(djangoforms.ModelForm): 
    class Meta: 
     model = birthDB.BirthDetails 

class SimpleInput(webapp.RequestHandler): 
    def get(self): 
     html = template.render('templates/header.html', {'title': 'Provide your birth details'}) 
     html = html + template.render('templates/form_start.html', {}) 
     html = html + str(BirthDetailsForm(auto_id=False))  
     html = html + template.render('templates/form_end.html', {'sub_title': 'Submit Details'}) 
     html = html + template.render('templates/footer.html', {'links': ''}) 
     self.response.out.write(html) 

def main(): 
    app = webapp.WSGIApplication([('/.*', SimpleInput)], debug=True) 
    wsgiref.handlers.CGIHandler().run(app) 

if __name__ == '__main__': 
    main() 

這裏是另一個Python模塊導入到一個以上稱爲birthDB.py:

from google.appengine.ext import db 

class BirthDetails(db.Model): 
    name =   db.StringProperty() 
    date_of_birth = db.DateProperty() 
    time_of_birth = db.TimeProperty() 

有一個模板上述Python模塊調用的文件夾。他們用一些Django代碼在他們的HTML代碼中。以下是使用footer.html的示例。

<p> 
{{ links }} 
</p> 
</body> 
</html> 

其他HTML文件是相似的。使用BASH命令可以毫無問題地啓動Google App Engine:python google_appengine/dev_appserver.py ~/Desktop/three目錄三個包含模板文件夾,app.yaml文件,Python模塊如上所示。

我的問題是,當我在http://localhost:8080沒有訪問網頁是存在的,在運行命令來啓動該BASH外殼顯示了導致問題的Python程序所有的呼叫,然後最後說:導入錯誤:由於環境變量DJANGO_SETTINGS_MODULE未定義,因此無法導入設置。

我已經在幾個不同的地方閱讀了幾個不同的東西來嘗試,所以我想我會繼續前進,並發表一篇新文章,並希望一些專家的Python程序員可以參加並幫助失去的霍比特人,比如作爲我自己。

此外,本書說要安裝Python2.5以使用此代碼,但Google App Engine現在支持Python2.7,該書在編寫書籍時不可用。另外,我剛剛檢查並且Python2.5甚至不適用於Google App Engine。 Python2.5 deprecated

這可能太複雜了,無法在這裏解決。我很驚訝所有這些不同的技術都用於Head First Python一書。它要求很多Python noob。^_^

問候,

user_loser

更新 - 我在我的Ubuntu操作系統

安裝的Django當我更改Python模塊368B線。PY from google.appengine.ext.db import djangoformsfrom django import forms我收到以下錯誤控制檯上訪問時,對localhost的網頁:從運行程序作爲是

[email protected]:~/Desktop$ google_appengine/dev_appserver.py three 
INFO  2014-09-06 21:08:36,669 api_server.py:171] Starting API server at:  http://localhost:56044 
INFO  2014-09-06 21:08:36,677 dispatcher.py:183] Starting module "default" running at: http://localhost:8080 
INFO  2014-09-06 21:08:36,678 admin_server.py:117] Starting admin server at: http://localhost:8000 
ERROR 2014-09-06 21:08:48,942 cgi.py:121] Traceback (most recent call last): 
File "/home/loser/Desktop/three/page368b.py", line 13, in <module> 
class BirthDetailsForm(forms.ModelForm): 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/models.py", line 205, in __new__ 
opts.exclude, opts.widgets, formfield_callback) 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/models.py", line 145, in fields_for_model 
opts = model._meta 
AttributeError: type object 'BirthDetails' has no attribute '_meta' 

INFO  2014-09-06 21:08:48,953 module.py:652] default: "GET/HTTP/1.1" 500 - 
ERROR 2014-09-06 21:08:49,031 cgi.py:121] Traceback (most recent call last): 
File "/home/loser/Desktop/three/page368b.py", line 13, in <module> 
class BirthDetailsForm(forms.ModelForm): 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/models.py", line 205, in __new__ 
opts.exclude, opts.widgets, formfield_callback) 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/models.py", line 145, in fields_for_model 
opts = model._meta 
AttributeError: type object 'BirthDetails' has no attribute '_meta' 

更新錯誤,不做任何更改:

[email protected]:~/Desktop$ google_appengine/dev_appserver.py three/ 
INFO  2014-09-06 21:35:19,347 api_server.py:171] Starting API server at: http://localhost:60503 
INFO  2014-09-06 21:35:19,356 dispatcher.py:183] Starting module "default" running at: http://localhost:8080 
INFO  2014-09-06 21:35:19,358 admin_server.py:117] Starting admin server at: http://localhost:8000 
ERROR 2014-09-06 21:35:25,011 cgi.py:121] Traceback (most recent call last): 
File "/home/loser/Desktop/three/page368b.py", line 13, in <module> 
class BirthDetailsForm(djangoforms.ModelForm): 
File "/home/loser/Desktop/google_appengine/google/appengine/ext/db/djangoforms.py", line 772, in __new__ 
form_field = prop.get_form_field() 
File "/home/loser/Desktop/google_appengine/google/appengine/ext/db/djangoforms.py", line 370, in get_form_field 
return super(DateProperty, self).get_form_field(**defaults) 
File "/home/loser/Desktop/google_appengine/google/appengine/ext/db/djangoforms.py", line 353, in get_form_field 
return super(DateTimeProperty, self).get_form_field(**defaults) 
File "/home/loser/Desktop/google_appengine/google/appengine/ext/db/djangoforms.py", line 200, in get_form_field 
return form_class(**defaults) 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/fields.py", line 340, in __init__ 
super(DateField, self).__init__(*args, **kwargs) 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/fields.py", line 99, in __init__ 
widget = widget() 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/forms/widgets.py", line 382, in __init__ 
self.format = formats.get_format('DATE_INPUT_FORMATS')[0] 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/utils/formats.py", line 67, in get_format 
if use_l10n or (use_l10n is None and settings.USE_L10N): 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/utils/functional.py", line 276, in __getattr__ 
self._setup() 
File "/home/loser/Desktop/google_appengine/lib/django-1.3/django/conf/__init__.py", line 40, in _setup 
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) 
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 
+0

你把settings.py文件放在哪裏? – power 2014-09-01 22:50:05

+0

這裏是一個示例設置(不是我的):http://django-rocket-engine.readthedocs.org/en/latest/installation.html – power 2014-09-01 22:52:15

+0

@power沒有settings.py - 是的,我已閱讀關於此文件但書中的例子中並沒有包含任何內容。但是這本書是在Python2.5仍然在Google App Engine中使用時編寫的。 – 2014-09-01 22:56:08

回答

2

我假設你正在使用本書附帶的示例代碼,可從本網站獲得:http://examples.oreilly.com/0636920003434/

如果您下載並展開第10章存檔(chapter10.zip),則會看到幾個示例文件以及幾個.zip存檔。 page368b.py文件對應於webapp-chapter10-simpleform.zip存檔。打開該存檔以創建webapp-chapter10-simpleform目錄。該目錄包含app.yaml文件,simpleform.py文件(與page368b.py相同),birthDB.pyext.db模型類)以及靜態和模板文件目錄。

不幸的是,正如您可能已經注意到的那樣,該示例並不適用於最新的SDK。 (我使用剛剛發佈的SDK 1.9.10版本。)當您嘗試加載頁面時,它會報告「ImportError:No module named django.core.exceptions」。嚴格地說,這個例子是而不是是一個Django應用程序,但僅僅試圖使用一個依賴於Django的庫。

Python 2.5運行時環境由本示例中包含的app.yaml文件選擇,默認情況下包含Django 0.96。但是,自從Head First Python被寫入以後,此行爲在SDK中發生了變化。最小的修復讓這個例子來工作是這些線路的djangoforms進口之前添加到simpleform.py

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

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

然後創建一個在應用程序的根目錄下名爲settings.py文件(webapp-chapter10-simpleform目錄)。在這種情況下,此文件可以爲空。 (正如其他評論者所指出的那樣,有一種更好的方法來生成使用Django框架時,該文件,但在這種情況下,我們只需要導入成功。)


要升級這個例子中使用Python 2.7運行時環境,修改app.yaml如下:

application: simpleform 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: true 

handlers: 
- url: /static 
    static_dir: static 

- url: /*. 
    script: simpleform.app 

的變化:

  • runtime:現在python27
  • threadsafe: true被添加
  • simpleform.py的引用現在是到全局變量app的Python對象路徑。

然後修改simpleform.py所以SimpleInput類的定義如下,一切都被替換爲:

app = webapp.WSGIApplication([('/.*', SimpleInput)], debug=True) 

,而不是運行simpleform.py腳本,Python的2.7運行時環境的進口它作爲一個模塊,然後查找用於全局變量中的WSGI應用程序對象。我們刪除的代碼執行了應用程序,現在已經由運行時環境爲我們完成了。

從這裏,您可以像使用libraries:子句一樣選擇較新版本的Django,如果您願意的話。在開發服務器中進行的快速測試表明,修改過的示例與最新支持的版本Django 1.5一起使用。

+0

這個例子有點過時的另一個方面是使用'ext.db'庫。如果您希望在完成本章後繼續深入研究,則可能需要查看App Engine中隨附的'ndb'庫,並在官方App Engine文檔中進行了描述。不過,沒有更多內置的'djangoforms'支持。 https://developers.google.com/appengine/docs/python/ndb/ – 2014-09-09 02:56:52

+0

另外值得注意的是:App Engine SDK將使用其自己的Django版本,而不是您在Ubuntu機器上安裝的版本。 SDK包含支持的Django版本。 (我不知道未來版本是否會出現這種情況。) – 2014-09-09 03:02:34

+0

感謝您的幫助!這適用於我的機器。我現在可以在第369頁上看到該表單了。您是Python + Google App Engine magus。另外,我可以像你一樣在Google工作嗎?感謝您再次致電我的Python編碼賞金。:D – 2014-09-13 20:58:20