Google App Engine 的项目结构 [英] Project structure for Google App Engine

查看:23
本文介绍了Google App Engine 的项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google App Engine 推出时立即启动了一个应用程序,以使用该技术并致力于一个我已经考虑了很长时间但从未开始的宠物项目.结果是 BowlSK.然而,随着它的发展和功能的增加,让事情变得井井有条变得非常困难 - 主要是因为这是我的第一个 python 项目,在我开始工作之前我对它一无所知.

I started an application in Google App Engine right when it came out, to play with the technology and work on a pet project that I had been thinking about for a long time but never gotten around to starting. The result is BowlSK. However, as it has grown, and features have been added, it has gotten really difficult to keep things organized - mainly due to the fact that this is my first python project, and I didn't know anything about it until I started working.

我有什么:

  • 主关卡包含:
    • 所有 .py 文件(不知道如何使包工作)
    • 主要级别页面的所有 .html 模板
    • 为 css、图像、js 等设置单独的文件夹
    • 包含子目录类型 url 的 .html 模板的文件夹

    示例:
    http://www.bowlsk.com/ 映射到主页(默认包),模板在index.html""
    http://www.bowlsk.com/games/view-series.html?series=7130 映射到 ViewSeriesPage(同样是默认包),模板在games/view-series.html"

    Example:
    http://www.bowlsk.com/ maps to HomePage (default package), template at "index.html"
    http://www.bowlsk.com/games/view-series.html?series=7130 maps to ViewSeriesPage (again, default package), template at "games/view-series.html"

    真恶心.我该如何重组?我有两个想法:

    It's nasty. How do I restructure? I had 2 ideas:

    • 主文件夹包含:appdef、indexes、main.py?

    • Main Folder containing: appdef, indexes, main.py?

    • 代码子文件夹.这一定是我的第一个包裹吗?
    • 模板的子文件夹.文件夹层次结构将匹配包层次结构
    • 用于 css、图像、js 等的单独子文件夹.

    包含 appdef、索引、main.py 的主文件夹?

    Main Folder containing appdef, indexes, main.py?

    • 代码 + 模板的子文件夹.这样我就在模板旁边有处理程序类,因为在这个阶段,我添加了很多功能,所以对一个的修改意味着对另一个的修改.同样,我是否必须将此文件夹名称作为我的类的第一个包名称?我希望文件夹为src",但我不希望我的类为src.WhateverPage"

    有最佳实践吗?随着 Django 1.0 的出现,当它成为官方的 GAE 模板引擎时,我现在可以做些什么来提高我的集成能力?我会简单地开始尝试这些东西,看看哪个看起来更好,但 pyDev 的重构支持似乎不能很好地处理包移动,所以让所有这些重新工作可能是一项重要的任务.

    Is there a best practice? With Django 1.0 on the horizon, is there something I can do now to improve my ability to integrate with it when it becomes the official GAE templating engine? I would simply start trying these things, and seeing which seems better, but pyDev's refactoring support doesn't seem to handle package moves very well, so it will likely be a non-trivial task to get all of this working again.

    推荐答案

    首先,我建议你看看 "使用 Python、Django 和 Google App Engine 进行快速开发"

    First, I would suggest you have a look at "Rapid Development with Python, Django, and Google App Engine"

    GvR 在其 幻灯片演示.

    在这里,我将发布该页面布局/结构的略微修改版本.我自己几乎遵循这种模式.您还提到您在打包时遇到了问题.只需确保您的每个子文件夹都有一个 __init__.py 文件.如果是空的就可以了.

    Here I'll post a slightly modified version of the layout/structure from that page. I pretty much follow this pattern myself. You also mentioned you had trouble with packages. Just make sure each of your sub folders has an __init__.py file. It's ok if its empty.

    • 这些在项目之间几乎没有变化
    • app.yaml:将所有非静态请求定向到 main.py
    • main.py:初始化应用程序并向其发送所有请求
    • static/*:静态文件;由 App Engine 直接提供
    • myapp/*.py:特定于应用程序的python代码
      • views.py、models.py、tests.py、__init__.py 等

      以下是一些可能也有帮助的代码示例:

      Here are some code examples that may help as well:

      import wsgiref.handlers
      
      from google.appengine.ext import webapp
      from myapp.views import *
      
      application = webapp.WSGIApplication([
        ('/', IndexHandler),
        ('/foo', FooHandler)
      ], debug=True)
      
      def main():
        wsgiref.handlers.CGIHandler().run(application)
      

      myapp/views.py

      import os
      import datetime
      import logging
      import time
      
      from google.appengine.api import urlfetch
      from google.appengine.ext.webapp import template
      from google.appengine.api import users
      from google.appengine.ext import webapp
      from models import *
      
      class IndexHandler(webapp.RequestHandler):
        def get(self):
          date = "foo"
          # Do some processing        
          template_values = {'data': data }
          path = os.path.join(os.path.dirname(__file__) + '/../templates/', 'main.html')
          self.response.out.write(template.render(path, template_values))
      
      class FooHandler(webapp.RequestHandler):
        def get(self):
          #logging.debug("start of handler")
      

      myapp/models.py

      from google.appengine.ext import db
      
      class SampleModel(db.Model):
      

      我认为这种布局非常适合新的和相对中小型的项目.对于较大的项目,我建议将视图和模型分解为拥有自己的子文件夹,例如:

      I think this layout works great for new and relatively small to medium projects. For larger projects I would suggest breaking up the views and models to have their own sub-folders with something like:

      • static/:静态文件;由 App Engine 直接提供服务
        • js/*.js
        • 图像/*.gif|png|jpg
        • css/*.css
        • 模型/*.py
        • 视图/*.py
        • 测试/*.py
        • templates/*.html:模板

        这篇关于Google App Engine 的项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆