lost password?

home
•  xaraya
•  rails +
•  django +
•  webdev
•  xamp
•  musings

rss
Tag this page
   

» Blogs that link here
last modified: Mar 22, 2007
(first posted: Mar 06, 2007)
(3353 Reads)
Permalink

Rails and Django - Installation / Directory Structure (part 5/15)

A technical manager's guide to evaluating web development frameworks, with a detailed review of Ruby on Rails and the Django (Python) projects. (Start here: Whitepaper index)

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
Using the plugin structure (with some extra monkey patching) you can actually create a sort of module system. We've done so, Revolution on Rails are talking about a similar system (plugems) and Rails Engines (an open source plugin although a bit different) So it's very possible to have a more modular system where you group controllers/models/views. It's just that the core team doesn't see it that way...

#

Post a new comment

: This is not spam

Name :