Rails and Django - Installation / Directory Structure (part 5/15)
I am not attempting to provide a manual for the frameworks but I do believe that looking into the generated file system directory structure can provide some useful insight.
I won't go into installation, my experience with each was comparable and pretty uneventful. I did blog the details at:
Note: Ruby files have the .rb extension, Python files have .py.
Both Rails and Django come with a lightweight, single process web server for development in your local environment.
Rails
To start a Rails project you use the command:
$ rails myproject
After generating a new project you'll find a full and rich set of subdirectories all ready for you to play in.
myproject/
app/ your application files
controllers/ your controller classes
helpers/ your helper functions
models/ your model classes
views/ your view templates
layouts/ your page layout templates
components/
config/ configuration files
db/ your database migration files
doc/ generated docs
lib/ additional application code
log/ log files
public/ images, javascript, stylesheets
script/ rails ruby scripts
test/ your unit, functional, integration tests, and fixtures
tmp/
vendor/ plugins
Rakefile
README
Under the app/ directory, your controllers, models, and views are kept together in their separate subdirectories.
To generate a new model:
$ ruby script/generate model mymodel
Which produces additional files:
myproject/
app/
models/
mymodel.rb
test/
unit/
mymodel_test.rb
fixtures/
mymodels.yml
db/
migrate/
001_create_mymodels.rb
First of all, if you don't want to write any templates, especially when first starting out a project, you can utilize the Rails built-in “scaffolding” generator, which will make everything you need (controller and views) on the fly from a Model. Scaffolding is widely hailed as a great thing about Rails. But its important to keep in mind that its really just to be used temporarily until you write your real code and templates. Scaffolding is not intended to be used in your final application.
To generate a new controller:
$ ruby script/generate controller mycontr
Which produces additional files:
myproject/
app/
controllers/
mycontr_controller.rb
helpers/
mycontr_helper.rb
views/
mycontr/
test/
functional/
mycontr_controller_test.rb
Most of the files are stubs, containing only basic hints. You proceed to edit the files and write code to build your application.
Django
To start a Django project:
$ django-admin.py startproject myproject
After generating a new project, the directory contains the following files:
myproject/
__init__.py
manage.py django script for this project
settings.py django settings
urls.py django URLconf (dispatch controller)
Then create an app:
$ python manage.py startapp myapp
Which produces the following files:
myproject/
myapp/
__init__.py
models.py your models
views.py your views
The __init__.py files are required by Python to recognize the directory as a valid module (library). Depending how you want to set things up, you might add a templates/ directory under myapp/, or you could put your templates in another place altogether.
Thus, generally the models, views, and templates are kept together under the app. This allows apps to be modular and pluggable, and transferable to other Django installations.
Opinion
The Rails project directory makes it very clear that there's a place for everything, and everything in its place. Files are organized by type-- views, controllers, models, etc. which means sections of your site could be spread across various directories. If you have a lot of files and want to modularize the code, you can use subdirectories. Still, a set of related files (model, controller, templates) are spread about in different directories, which seems upside down to me. But the organization is clean, and with Rails' file naming conventions, it's probably not much of a problem (no doubt this explains why TextMate on the Mac is a preferred editor, as it includes an integrated file explorer).
In Django, the default directories are sparse. You get the minimum you need to get started. No more, no less.
The organization of Django's apps seems more intuitive to me than spreading related files across multiple directories, and seems to lend itself better to application level modularity. (On the other hand, Rails has a much stronger plug-in capability, and a growing repository of reusable Plugins that Django sorely lacks).
Conclusion: Django's simple unobtrusive directory structure is best.
| Installation / Directory Structure | Rails | Django |
| Installation | easy | easy |
| Directories | strict, thorough, by function | flexible, sparse, by app |
| MY RATING: (1=worse, 5=best) | 4 | 5 |




Rails and Django - Installation / Directory Structure (part 5/15)
Posted by: Marcus on May 06, 2007 06:31 AM#