Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add model forms by Django in Google app engine?

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:

  1. How to use Django library to solve my problem?

  2. If it is possible, do I have to make my website a project, and create ten apps for models?

  3. Do I have to modify my yaml file, and create new url.py, setting.py, etc?

I really appreciate for any comment and suggestion. I am using Python 2.7, GAE 1.6.2 and Django 1.2.

Here is the code (code 1) with problem.

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).

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

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

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 -
like image 829
TTT Avatar asked Feb 24 '12 19:02

TTT


1 Answers

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:

http://code.google.com/p/rietveld/source/diff?spec=svn33bb90856ee02a26fa70c873658e91df445aca93&r=33bb90856ee02a26fa70c873658e91df445aca93&format=side&path=/codereview/views.py

like image 73
Guido van Rossum Avatar answered Oct 03 '22 05:10

Guido van Rossum