<p>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 <a href="http://www.bowlsk.com" rel="noreferrer">BowlSK</a>.  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.</p>

<p>What I have:</p>

<ul>
<li>Main Level contains:

<ul>
<li>all .py files (didn't know how to make packages work)</li>
<li>all .html templates for main level pages</li>
</ul></li>
<li>Subdirectories:

<ul>
<li>separate folders for css, images, js, etc.</li>
<li>folders that hold .html templates for subdirecty-type urls</li>
</ul></li>
</ul>

<p>Example:<br/>
<a href="http://www.bowlsk.com/" rel="noreferrer">http://www.bowlsk.com/</a> maps to HomePage (default package), template at "index.html"<br/>
<a href="http://www.bowlsk.com/games/view-series.html?series=7130" rel="noreferrer">http://www.bowlsk.com/games/view-series.html?series=7130</a> maps to ViewSeriesPage (again, default package), template at "games/view-series.html"</p>

<p>It's nasty.  How do I restructure?  I had 2 ideas:</p>

<ul>
<li><p>Main Folder containing: appdef, indexes, main.py?</p>

<ul>
<li>Subfolder for code.  Does this have to be my first package?</li>
<li>Subfolder for templates.  Folder heirarchy would match package heirarchy</li>
<li>Individual subfolders for css, images, js, etc.</li>
</ul></li>
<li><p>Main Folder containing appdef, indexes, main.py?</p>

<ul>
<li>Subfolder for code + templates.  This way I have the handler class right next to the template, because in this stage, I'm adding lots of features, so modifications to one mean modifications to the other.  Again, do I have to have this folder name be the first package name for my classes?  I'd like the folder to be "src", but I don't want my classes to be "src.WhateverPage"</li>
</ul></li>
</ul>

<p>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.</p>

[{'answer_id': 48467, 'author': 'Dominic Eidson', 'author_id': 5042, 'author_profile': 'https://Stackoverflow.com/users/5042', 'pm_score': 1, 'selected': False, 'text': '<p>I am not entirely up to date on the latest best practices, et cetera when it comes to code layout, but when I did my first GAE application, I used something along your second option, where the code and templates are next to eachother.</p>\n\n<p>There was two reasons for this - one, it kept the code and template nearby, and secondly, I had the directory structure layout mimic that of the website - making it (for me) a bit easier too remember where everything was.</p>\n'}, {'answer_id': 67533, 'author': 'Jiayao Yu', 'author_id': 10289, 'author_profile': 'https://Stackoverflow.com/users/10289', 'pm_score': 3, 'selected': False, 'text': '<p>I think the first option is considered the best practice. And make the code folder your first package. The Rietveld project developed by Guido van Rossum is a very good model to learn from. Have a look at it: <a href="http://code.google.com/p/rietveld" rel="nofollow noreferrer">http://code.google.com/p/rietveld</a></p>\n\n<p>With regard to Django 1.0, I suggest you start using the Django trunk code instead of the GAE built in django port. Again, have a look at how it\'s done in Rietveld.</p>\n'}, {'answer_id': 70271, 'author': 'fuentesjr', 'author_id': 10708, 'author_profile': 'https://Stackoverflow.com/users/10708', 'pm_score': 8, 'selected': True, 'text': '<p>First, I would suggest you have a look at "<a href="http://sites.google.com/site/io/rapid-development-with-python-django-and-google-app-engine" rel="noreferrer">Rapid Development with Python, Django, and Google App Engine</a>"</p>\n\n<p>GvR describes a general/standard project layout on page 10 of his <a href="http://sites.google.com/site/io/rapid-development-with-python-django-and-google-app-engine/rapid_development_with_django_gae.pdf?attredirects=0" rel="noreferrer">slide presentation</a>.  </p>\n\n<p>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.</p>\n\n<h2>Boilerplate files</h2>\n\n<ul>\n<li>These hardly vary between projects</li>\n<li>app.yaml: direct all non-static requests to main.py </li>\n<li>main.py: initialize app and send it all requests </li>\n</ul>\n\n<h2>Project lay-out</h2>\n\n<ul>\n<li>static/*: static files; served directly by App Engine</li>\n<li>myapp/*.py: app-specific python code\n\n<ul>\n<li>views.py, models.py, tests.py, __init__.py, and more</li>\n</ul></li>\n<li>templates/*.html: templates (or myapp/templates/*.html)</li>\n</ul>\n\n<p>Here are some code examples that may help as well:</p>\n\n<h2>main.py</h2>\n\n<pre><code>import wsgiref.handlers\n\nfrom google.appengine.ext import webapp\nfrom myapp.views import *\n\napplication = webapp.WSGIApplication([\n  (\'/\', IndexHandler),\n  (\'/foo\', FooHandler)\n], debug=True)\n\ndef main():\n  wsgiref.handlers.CGIHandler().run(application)\n</code></pre>\n\n<h2>myapp/views.py</h2>\n\n<pre><code>import os\nimport datetime\nimport logging\nimport time\n\nfrom google.appengine.api import urlfetch\nfrom google.appengine.ext.webapp import template\nfrom google.appengine.api import users\nfrom google.appengine.ext import webapp\nfrom models import *\n\nclass IndexHandler(webapp.RequestHandler):\n  def get(self):\n    date = "foo"\n    # Do some processing        \n    template_values = {\'data\': data }\n    path = os.path.join(os.path.dirname(__file__) + \'/../templates/\', \'main.html\')\n    self.response.out.write(template.render(path, template_values))\n\nclass FooHandler(webapp.RequestHandler):\n  def get(self):\n    #logging.debug("start of handler")\n</code></pre>\n\n<h2>myapp/models.py</h2>\n\n<pre><code>from google.appengine.ext import db\n\nclass SampleModel(db.Model):\n</code></pre>\n\n<p>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:</p>\n\n<h2>Project lay-out</h2>\n\n<ul>\n<li>static/: static files; served directly by App Engine\n\n<ul>\n<li>js/*.js</li>\n<li>images/*.gif|png|jpg</li>\n<li>css/*.css</li>\n</ul></li>\n<li>myapp/: app structure\n\n<ul>\n<li>models/*.py</li>\n<li>views/*.py</li>\n<li>tests/*.py</li>\n<li>templates/*.html: templates</li>\n</ul></li>\n</ul>\n'}, {'answer_id': 153862, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 4, 'selected': False, 'text': "<p>My usual layout looks something like this:</p>\n\n<ul>\n<li>app.yaml</li>\n<li>index.yaml</li>\n<li>request.py - contains the basic WSGI app</li>\n<li>lib\n\n<ul>\n<li><code>__init__.py</code> - common functionality, including a request handler base class</li>\n</ul></li>\n<li>controllers - contains all the handlers. request.yaml imports these.</li>\n<li>templates\n\n<ul>\n<li>all the django templates, used by the controllers</li>\n</ul></li>\n<li>model\n\n<ul>\n<li>all the datastore model classes</li>\n</ul></li>\n<li>static\n\n<ul>\n<li>static files (css, images, etc). Mapped to /static by app.yaml</li>\n</ul></li>\n</ul>\n\n<p>I can provide examples of what my app.yaml, request.py, lib/<strong>init</strong>.py, and sample controllers look like, if this isn't clear.</p>\n"}, {'answer_id': 3105295, 'author': 'systempuntoout', 'author_id': 130929, 'author_profile': 'https://Stackoverflow.com/users/130929', 'pm_score': 2, 'selected': False, 'text': '<p>I like <a href="http://webpy.org/" rel="nofollow noreferrer">webpy</a> so I\'ve adopted it as templating framework on Google App Engine.<br>\nMy package folders are typically organized like this:</p>\n\n<pre><code>app.yaml\napplication.py\nindex.yaml\n/app\n   /config\n   /controllers\n   /db\n   /lib\n   /models\n   /static\n        /docs\n        /images\n        /javascripts\n        /stylesheets\n   test/\n   utility/\n   views/\n</code></pre>\n\n<p><a href="http://github.com/systempuntoout/stackprinter" rel="nofollow noreferrer">Here</a> is an example.</p>\n'}, {'answer_id': 12535000, 'author': 'sunil', 'author_id': 278681, 'author_profile': 'https://Stackoverflow.com/users/278681', 'pm_score': 4, 'selected': False, 'text': '<p>I implemented a google app engine boilerplate today and checked it on github. This is along the lines described by Nick Johnson above (who used to work for Google).</p>\n\n<p>Follow this link <a href="https://github.com/droot/gae-boilerplate">gae-boilerplate</a></p>\n'}]
['https://Stackoverflow.com/questions/48458', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/96/']