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
# creating a Django project
django-admin.py startproject django_project
Lets look at the files and structure created by the respective frameworks
that is huge….
lets look at the django project files
far lesser when compared to the rails project.
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
even after including this, the number of files is still less than the rails project.
Lets break it down and relate both the frameworks.
config/database.yml for database settings
development.rb for development specific settings
production.rb for production specific settings
test.rb for test specific settings
settings.py, one file for everything, database configuration and any other configuaration or settings will be in this file
db/schema.rb for the ruby version of db schema
app/models/* for the models
complete schema is not stored any where
Under every application in your project, models.py file will contain the table specific schema stored as django models
./script/server, start server
./script/console, ruby console
./script/dbconsole, database console
rake db:migrate, run database migrations
manage.py is the file for all your tasks
./manage.py runserver, start server
./manage.py shell, python console
./manage.py dbshell, database console
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
app/views//* is the place for the templates
default: looks inside 'templates' directory, under the application directory
looks in the directory specified by 'TEMPLATE_DIRS' in settings.py
and lets have a look at the other things in rails_project
Missing pieces in Django (for a rails developer)
- 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.
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):
Admin (almost everybodys favourite)
Generic Views, helps from writing a lot of repetetive code, James Bennett's blog on generic views
Django DB API & QuerySets, (chaining and the filters that are missing in ActiveRecord)
Forms and above all, my favorite yet: ModelForm.
You can get the migrations feature in Django using South. Being an external app, it a bit of pain in setting it up to work with South. Other than that it is more like rails migrations
Search the Django way
Following are the questions that I keep getting,
When a project will almost have a application why creating project & app has to be two different steps?
Why not create a 'templates' directoy and a 'base.html' either in project's directory or in the apps's directory, because creating the same templates directory and same base.html for every project is not DRY :) ?
Why serving static files in development has to be a additional setup, as no developer wants to setup a server for serving static files, I am aware of 'django.static.serve' but still that is an additional setup, why not create a sample media directory and a url for the same in urls.py ?
let me know via comments, if you have any answers
program used for listing the files in a directory
directory = sys.argv
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:
print " " * indent + os.path.split(item) + '/'
print " " * indent + os.path.split(item)
- Scaffold magic
- Generate commands
Thank you for reading the Agiliq blog. This article was written by dheeru on Nov 26, 2009 in
You can subscribe ⚛ to our blog.
We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.
Would you like to download 10+ free Django and Python books?
Get them here