我正在閱讀'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 djangoforms
到from 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.
你把settings.py文件放在哪裏? – power 2014-09-01 22:50:05
這裏是一個示例設置(不是我的):http://django-rocket-engine.readthedocs.org/en/latest/installation.html – power 2014-09-01 22:52:15
@power沒有settings.py - 是的,我已閱讀關於此文件但書中的例子中並沒有包含任何內容。但是這本書是在Python2.5仍然在Google App Engine中使用時編寫的。 – 2014-09-01 22:56:08