I have built a website based on Google App Engine. It includes ten models, doing some simple calculations based on user inputs. Previously, it was coded using Python 2.5 using djangoform.modelform and db.model to handle data. Since Python 2.7 does not support djangoform.modelform, I would like to use django modelform instead. However, during the migration, I met some problems(sever internal error). when I was trying to add a form by Django.forms.Modelform. I have provided my old codes and new one (does not work). My questions are:
我已经建立了一个基于Google App Engine的网站。它包括十个模型,根据用户输入进行一些简单的计算。以前,它使用Python 2.5使用djangoform.modelform和db.model进行编码来处理数据。由于Python 2.7不支持djangoform.modelform,我想改用django modelform。但是,在迁移过程中,我遇到了一些问题(严重的内部错误)。当我试图通过Django.forms.Modelform添加表单时。我提供了我的旧代码和新代码(不起作用)。我的问题是:
-
How to use Django library to solve my problem?
如何使用Django库来解决我的问题?
-
If it is possible, do I have to make my website a project, and create ten apps for models?
如果有可能,我是否必须使我的网站成为一个项目,并为模型创建十个应用程序?
-
Do I have to modify my yaml file, and create new url.py, setting.py, etc?
我是否必须修改我的yaml文件,并创建新的url.py,setting.py等?
I really appreciate for any comment and suggestion. I am using Python 2.7, GAE 1.6.2 and Django 1.2.
我非常感谢任何评论和建议。我使用的是Python 2.7,GAE 1.6.2和Django 1.2。
Here is the code (code 1) with problem.
这是有问题的代码(代码1)。
import webapp2 as webapp
import django
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
from django.db import models
from django.forms import ModelForm
class trexInp(models.Model):
chemical_name = models.CharField(max_length=255)
class trexInput(ModelForm):
class Meta:
model = trexInp
class trexInputPage(webapp.RequestHandler):
def get(self):
html = str(trexInput())
self.response.out.write(html)
app = webapp.WSGIApplication([('/.*', trexInputPage)], debug=True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
To compare, I have attached the old code (Code 2).
为了比较,我附上了旧代码(代码2)。
import webapp2 as webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
class trexInp(db.Model):
chemical_name = db.StringProperty()
class trexInput(djangoforms.ModelForm):
class Meta:
model = trexInp
class trexInputPage(webapp.RequestHandler):
def get(self):
html = str(trexInput())
self.response.out.write(html)
app = webapp.WSGIApplication([('/.*', trexInputPage)], debug=True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
Here is my app.yaml file
这是我的app.yaml文件
application: pypest1
version: 1
runtime: python27
api_version: 1
threadsafe: false
libraries:
- name: numpy
version: latest
- name: webapp2
version: latest
- name: django
version: "1.2"
handlers:
- url: /
script: main.py
- url: /index.html
script: main.py
#t-rex
- url: /trex_input.html
script: trex/trex_input.py
My main.py file import os os.environ['DJANGO_SETTINGS_MODULE']='global_settings' import webapp2 as webapp
我的main.py文件导入os os.environ ['DJANGO_SETTINGS_MODULE'] ='global_settings'导入webapp2为webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
class defaultPage(webapp.RequestHandler):
def get(self):
html = template.render('templates/01.html', {'title':'model'})
self.response.out.write(html)
app = webapp.WSGIApplication([('/.*', defaultPage)], debug=True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
Here is the server log:
这是服务器日志:
2012-02-24 22:40:55 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--admin_console_server=', '--port=8082', u'C:\\Users\\tao\\Dropbox\\AppPest1']"
WARNING 2012-02-25 03:40:57,216 rdbms_mysqldb.py:74] The rdbms API is not available because the MySQLdb library could not be loaded.
INFO 2012-02-25 03:40:57,486 dev_appserver_multiprocess.py:650] Running application dev~pypest1 on port 8082: http://localhost:8082
INFO 2012-02-25 03:40:57,486 dev_appserver_multiprocess.py:652] Admin console is available at: http://localhost:8082/_ah/admin
WARNING 2012-02-25 03:40:59,211 py_zipimport.py:139] Can't open zipfile C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info: IOError: [Errno 13] file not accessible: 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'
INFO 2012-02-25 03:40:59,611 dev_appserver.py:2865] "GET / HTTP/1.1" 200 -
INFO 2012-02-25 03:40:59,671 dev_appserver.py:2865] "GET /stylesheets/style.css HTTP/1.1" 304 -
INFO 2012-02-25 03:40:59,720 dev_appserver.py:2865] "GET /images/valid-xhtml10-blue.png HTTP/1.1" 304 -
INFO 2012-02-25 03:40:59,767 dev_appserver.py:2865] "GET /images/valid-css-blue.png HTTP/1.1" 304 -
INFO 2012-02-25 03:40:59,816 dev_appserver.py:2865] "GET /stylesheets/images/bg.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:40:59,867 dev_appserver.py:2865] "GET /stylesheets/images/header.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:41:00,023 dev_appserver.py:2865] "GET /stylesheets/images/intro.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:41:00,381 dev_appserver.py:2865] "GET /favicon.ico HTTP/1.1" 404 -
INFO 2012-02-25 03:41:01,177 dev_appserver.py:2865] "GET /trex_description.html HTTP/1.1" 200 -
INFO 2012-02-25 03:41:01,538 dev_appserver.py:2865] "GET /stylesheets/style.css HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,585 dev_appserver.py:2865] "GET /images/valid-xhtml10-blue.png HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,632 dev_appserver.py:2865] "GET /images/valid-css-blue.png HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,783 dev_appserver.py:2865] "GET /stylesheets/images/bg.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,891 dev_appserver.py:2865] "GET /stylesheets/images/header.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,940 dev_appserver.py:2865] "GET /stylesheets/images/intro.jpg HTTP/1.1" 304 -
INFO 2012-02-25 03:41:01,996 dev_appserver.py:2865] "GET /favicon.ico HTTP/1.1" 404 -
WARNING 2012-02-25 03:41:03,632 py_zipimport.py:139] Can't open zipfile C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info: IOError: [Errno 13] file not accessible: 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'
ERROR 2012-02-25 03:41:03,657 cgi.py:121] Traceback (most recent call last):
File "C:\Users\tao\Dropbox\AppPest1\trex\trex_input.py", line 33, in <module>
class trexInp(models.Model):
File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_2\django\db\models\base.py", line 50, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
INFO 2012-02-25 03:41:03,686 dev_appserver.py:2865] "GET /trex_input.html HTTP/1.1" 500 -
1 个解决方案
#1
5
I can't help you with your specific code, but I had to do a similar task (replace djangoforms) myself recently. This is the change that I cam up with. Hopefully you can learn something from it:
我无法帮助你解决你的具体代码,但我最近必须自己做一个类似的任务(替换djangoforms)。这是我所提出的变化。希望你能从中学到一些东西:
#1
5
I can't help you with your specific code, but I had to do a similar task (replace djangoforms) myself recently. This is the change that I cam up with. Hopefully you can learn something from it:
我无法帮助你解决你的具体代码,但我最近必须自己做一个类似的任务(替换djangoforms)。这是我所提出的变化。希望你能从中学到一些东西: