views:

197

answers:

2

Hello,

I am relatively new to python and app engine, and I just finished my first project. It consists of several *.py files (usually py file for every page on the site) and respectively temple files for each py file. In addition, I have one big PY file that has many functions that are common to a lot of pages, in I also declared the classes of db.Model (that is the datastore kinds).

My question is what is the convention (if there is one) of arranging these files. If I create a model.py with the datastore classes, should it be in different package? Where should I put my template files and all of the py files that handle every page (should they be in the same directory as the one big common PY file)?

I have tried to look for MVC and such implementations online but there are very few.

Thanks,

Joel

+1  A: 

I usually organize my projects in this way:

project 
  main.py
  README
  models
      bar.py
      foo.py
  views
      foolist.hml
      barlist.hml
  controllers
      controller1.py
      controller2.py
  api
      controllerapi.py
  helpers
      utilities.py
  lib
      extfoo.py
  db
     foo.db
  test
     test.py

Look at this post; it's a really great article on how to structure a project (not in python but it does not matter).

systempuntoout
Thanks for the quick answer and the reference to a very good article!I would appreciate if you can clarify few things for me: 1) why would you use a different model file for each page? It seems like usually most pages use the same Kinds (models) in their pages so it is hard to split them.2) If I understand correctly - controller1.py will have a handler for foo and controll2 will have a handler for bar, right? Then if you want to make changes in the datastore for example, you would call a function in utilities.py. Did I get it right?Thanks!
Joel
Just focus on directories structure and not on what i put inside, it's just an example :).You could use just one file for all the models and one file for all the controllers! I tipically use a structure like this when i develop on web ( using gae\web.py).
systempuntoout
will do :) Just wanted to make sure if by "controllers" you mean the file who hold the handlers. And one last thing (I promise): Basically "controllers" and "models" are packages (which as far as I understand must contain _init_.py file) whereas views is a regular directory (no _init.py file), right?
Joel
You are right!.
systempuntoout
Thanks a lot! Really appreciated
Joel
One question on this, I have my structure like that also but when I make changes to any of the files inside a folder (models/aModel.py or controllers/aController.py) I need to restart dev_appserver to get it to pick up on those changes. It seems like it is doing some cacheing and I am not sure how to tell it not to do that. Any pointers?
Matt
+1  A: 

Typically I organize like so:

project/
  main.py
  models.py
  app.yaml
  index.yaml
  templates/
    main.html
    foo.html
    ...
  styles/
    project.css
  js/
    jquery.js
    project.js
  images/
    icon.png
    something.jpg

And I have all of my handlers in main.py, all of my models in models.py, etc.

If I have a lot of handlers, and I can easily split the functionality of some handlers from the others (like taskqueue handlers vs. request handlers vs. xmpp/email handlers) I'll add another foo_handlers.py to the mix, but usually I just cram them all in main.py

But then again, I tend not to write hugely complex Python App Engine apps...

Jason Hall
Thanks! very helpful.Where would you put your css "stylesheet" and "javascript" folders?
Joel
Edited my answer to include JS, CSS and images. My main point is that most handlers are fairly short and sweet, and will in fact get more complicated the more files you have to have open to do anything.
Jason Hall