Django for a Rails Developer
By : ashok
This is not yet another Django vs Rails blog post. It is a compilation of notes I made working with Django after having worked on Rails for years.
In this post I want to give a brief introduction to Django project layout from a Rails developer point of view, on what is there, what is not there and where to look for things. It should help a rails developer working on django be able to find the necessary files and underatnd the layout of the project files.
Once you have both the frameworks installed on your system you can create the projects using the commands
# creating a rails project
rails rails_project
# creating a Django project
django-admin.py startproject django_project
Lets look at the files and structure created by the respective frameworks
#rails_project
README
Rakefile
app/
controllers/
application_controller.rb
helpers/
application_helper.rb
models/
views/
layouts/
config/
boot.rb
database.yml
environment.rb
environments/
development.rb
production.rb
test.rb
initializers/
backtrace_silencers.rb
inflections.rb
mime_types.rb
new_rails_defaults.rb
session_store.rb
locales/
en.yml
routes.rb
db/
seeds.rb
doc/
README_FOR_APP
lib/
tasks/
log/
development.log
production.log
server.log
test.log
public/
404.html
422.html
500.html
favicon.ico
images/
rails.png
index.html
javascripts/
application.js
controls.js
dragdrop.js
effects.js
prototype.js
robots.txt
stylesheets/
script/
about
console
dbconsole
destroy
generate
performance/
benchmarker
profiler
plugin
runner
server
test/
fixtures/
functional/
integration/
performance/
browsing_test.rb
test_helper.rb
unit/
tmp/
cache/
pids/
sessions/
sockets/
vendor/
plugins/
that is huge....
lets look at the django project files
# django_project
__init__.py
manage.py
settings.py
urls.py
far lesser when compared to the rails project.
In fact a rails project comes with everything a web application needs. When I say everything I mean everything..... base application, routing, database configuration, development, test and production environment specific configurations and their respective log files, javascript, test, some standard html files and some helpful scripts for developing.
Why then does a Django project have so less number of files? It has got to do with the Django's philosophy and the concept of applications. The django project is not complete without the application, so lets create a application inside the project and have a look at the structure
django-admin.py startapp app
# django_project
__init__.py
app/
__init__.py
models.py
tests.py
views.py
manage.py
settings.py
urls.py
even after including this, the number of files is still less than the rails project.
Lets break it down and relate both the frameworks.
| Rails | Django | |
|---|---|---|
| Configuration Files |
|
settings.py, one file for everything, database configuration and any other configuaration or settings will be in this file |
| URLs | config/routes.rb | urls.py |
| Schema/Models |
|
|
| Management Commands |
|
manage.py is the file for all your tasks
|
| Application Code | app/controllers/* will contain the application logic | views.py file under each application folder is the place to write to your application logic, file can be named with any name, views.py is the general convention |
| Application templates |
app/views/ |
|
and lets have a look at the other things in rails_project
- Logging, unders the logs directory
- Some default html files for some standard http errors, under the public directory
- Rails has very good support for testing, for that bunch of files under tests
- Vendor/Plugin, place for some third party plugins/applications.
Missing pieces in Django (for a rails developer)
- Scaffold magic
- Generate commands
- Migrations
and what is Django is providing by default? Sorry no extra files in the project; but you will get an authentication system and Django's killer feature, admin, just by modifying your 'INSTALLED_APPS' in your settings. It is similar to the plugins feature in Rails, Django's philosophy of resusable apps helps you in getting any particular functionality into your project.
Following is a list of what I like in Django (and associated apps):
Following are the questions that I keep getting,
let me know via comments, if you have any answers
program used for listing the files in a directory
import os
import sys
try:
directory = sys.argv[1]
except IndexError:
directory = os.path.dirname(os.path.abspath(__file__))
def r_list_dir(directory, indent=0):
dir_files = sorted([os.path.join(directory, file_name) for file_name in os.listdir(directory)])
for item in dir_files:
if os.path.isdir(item):
print " " * indent + os.path.split(item)[1] + '/'
r_list_dir(item, indent+4)
else:
print " " * indent + os.path.split(item)[1]
r_list_dir(directory)
Related Posts
- Introduction to Python Workshop on February 15th, 2013
- Two Scoops of Django: Review
- Serving static files in Django
- Logging in Django
- Common testing scenarios for Django app.
Can we help you build amazing apps? Contact us today.
> let me know via comments, if you have any answers
For the record, I believe all the questions you asked are valid design decisions.