Django for a Rails Developer

by Ashok on November 26, 2009

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
  • config/database.yml for database settings
  • config/environments/
  • 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
    URLs config/routes.rb urls.py
    Schema/Models
  • 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
  • Management Commands
  • ./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
  • ./manage.py syncdb
  • 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//* 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

    • 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):

  • 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
  • django-command-extensions
  • 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

    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:

    1. Rails and Django commands : comparison and conversion
    2. The Rails and Django models layer Rosseta stone
    3. Tools of Pro Django developer – aka What powers dinette and almost every app we write.
    4. Great Indian Developer Summit 2010: A Review

    3 Comments 46 Tweets 41 Comments

    { 6 trackbacks }

    Django for a Rails Developer — The Usware Blog - Django Web … | Search engine optimization - SEO - Durban - KZN
    November 27, 2009 at 12:51 am
    Random Links #87 | YASDW - yet another software developer weblog
    November 27, 2009 at 11:14 am
    10 Sites Para Você Aprender Django com Aplicações, Livros, Vídeos, Slides e Tutoriais | PedroMenezes.com
    December 11, 2009 at 11:06 am
    Ennuyer.net » Blog Archive » Rails Reading - December 16, 2009
    December 16, 2009 at 8:11 am
    Delicious Bookmarks for January 12th from 17:07 to 17:11 « Lâmôlabs
    January 12, 2010 at 6:11 pm
    AGILIQ SOLUTIONS - Blog - Rails and Django commands : comparison and conversion
    June 8, 2010 at 8:02 am

    { 93 comments… read them below or add one }

    3 roam November 26, 2009 at 3:00 pm

    You’re right that it would be easier if some things were set to a reasonable default.But if you want it right now: create a custom command to do it your way and wrap it in an app that you add to the path. When you start a new project, the first thing you do is add the application to your installed apps and you can use it over and over again.

    This comment was originally posted on Hacker News

    4 tvon November 26, 2009 at 2:04 pm

    I work with Django for a living but the lack of init/buildout in a new project has always driven me a bit nuts, I guess it’s one of my development pet-peeves.I’ve never had project that didn’t need:

    - a project-wide templates/ directory- media/[css|img|js] directories- some kind of database setup by default- contrib.admin (well, I created a form mailer once that had no admin)

    Django seems to take a "we don’t want to force you to use any particular setup" stance, but the result seems to be to force you to make a bunch of relatively meaningless decisions before you can start writing code. (they could have other reasons, I haven’t looked into it lately)

    These days I have a script that does all this, and I know others have written similar scripts as well. It just strikes me as being a gaping hole on the Django development model… IMO anyway.

    This comment was originally posted on Hacker News

    5 kteague November 26, 2009 at 3:10 pm

    The question is, why use the framework to create the layout of project which happens to depend upon that framework at all? Instead, use a generic project templating tool to create the layout, then it’s easy to choose alternate starting points depending upon what you need in your project. In Python, ‘paster’ is commonly used for this. It lets you create projects that depend upon TurboGears or Zope or Plone or Grok or even plain old Python projects which don’t depend upon a full framework.

    This comment was originally posted on Hacker News

    6 kteague November 26, 2009 at 3:20 pm

    Also note that the default Django template is showing it’s age. I don’t think it’s ever been updated and it contains a few faux-pas to look out for. In particular, it starts you off inside a python package (__init__.py file), which is highly confusing. If you later want to treat your Django project as a normal Python project, you need to create a setup.py file. But this file would be one directory up from the project directory … which is outside of your Django project. Django puts the directory one level up on your PYTHONPATH to compensate for this, with the assumption that part of your project lives outside of version-control, etc. The solution is to remember to put the directory above the directory that the project lives in version-control and treat it as part of the project. Although then you have to put a library location on your PATH to run the manage.py commands, since this file won’t work if placed in your projects /bin directory.(Speaking of /bin directories, it’s always bugged me that Rails renamed this directory to /scripts … if it’s executable, it goes in /bin, it makes no sense to split executables based on arbitrary implementation details)

    This comment was originally posted on Hacker News

    7 carbon8 November 26, 2009 at 2:28 pm

    I went from django to rails and ruby. I still use python a lot and contribute a little to python open source projects, but I prefer ruby and the ruby frameworks for web development. On top of that, the widespread attitude in the django community that’s the source of comments like yours ensures that I will absolutely never go near django again.

    This comment was originally posted on Hacker News

    8 carbon8 November 26, 2009 at 2:28 pm

    I went from django to rails and ruby. I still use python a lot and contribute a little to python open source projects, but I prefer ruby and the ruby frameworks for web development. On top of that, the widespread attitude in the django community that you’re demonstrating ensures that I will absolutely never go near django again.

    This comment was originally posted on Hacker News

    9 carbon8 November 26, 2009 at 3:29 pm

    I went from django to rails and ruby. I still use python a lot and contribute a little to python open source projects, but I prefer ruby and the ruby frameworks for web development. On top of that, the widespread attitude in the django community that you’re demonstrating ensures that I, for one, will absolutely never go near django again.

    This comment was originally posted on Hacker News

    11 BerislavLopac November 26, 2009 at 4:10 pm

    Sometimes I wonder if they renamed the pattern just to keep up the music theme…

    This comment was originally posted on Hacker News

    12 ubernostrum November 26, 2009 at 4:31 pm

    And your script probably sets things up differently from everyone else’s :) But if you think it’s a problem, why not publish whatever you use and encourage others to use it as well?

    (also, FWIW I don’t have such a script — a new site at work always simply inherits default settings from other stuff, and those defaults are set up to match how our production servers work)

    This comment was originally posted on Hacker News

    13 ubernostrum November 26, 2009 at 4:33 pm

    Well, to be perfectly honest I almost never use the ’startproject’ or ’startapp’ commands. When I’m working on a new app I just create the directory and an __init__.py, and then start filling in specific files inside it as I need them.For stuff at work apps all go into one of a couple particular namespaces, so packaging concerns don’t come up there; for my personal stuff the package name is almost never the same as the app anyway (e.g., django-registration provides an app in a module named ‘registration’).

    Also, I’d really really like the concept of the project to die soon.

    This comment was originally posted on Hacker News

    15 artpop November 26, 2009 at 4:44 pm

    Seems like Django is more comparable to Sinatra? And is 4 spaces the python way? I like my font big and my lines short.

    This comment was originally posted on Hacker News

    18 iamwil November 26, 2009 at 4:38 pm

    The most annoying part about doing Django as a dev coming from Rails is the RequestContext that isn’t used by default, if you want to access specific contexts like settings. Then it means having to hack it using direct_to_template or writing a decorator.It’s a different philosophy, but it’s hard getting use to having to explicitly spell out everything when I just want to get going with defaults that make sense.

    This comment was originally posted on Hacker News

    19 laddy November 26, 2009 at 5:49 pm

    I would also like to see this.

    This comment was originally posted on Hacker News

    20 nailer November 26, 2009 at 5:52 pm

    Same here – MTC would have been simpler to understand for me.

    This comment was originally posted on Hacker News

    21 alexkay November 26, 2009 at 7:04 pm

    > And is 4 spaces the python way?It is, see PEP 8: http://www.python.org/dev/peps/pep-0008/

    This comment was originally posted on Hacker News

    22 rbanffy November 26, 2009 at 6:51 pm

    I admit. I mod up for funny.

    This comment was originally posted on Hacker News

    23 tvon November 26, 2009 at 7:57 pm

    > And your script probably sets things up differently from everyone else’s :) I don’t doubt it. Much like how there is the occasional "this is how I handle managing multiple settings for different systems" blog post, people have come up with their own systems because Django provides none.

    > But if you think it’s a problem, why not publish whatever you use and encourage others to use it as well?

    http://github.com/tvon/django-gig

    Written for personal use, so…

    There is also "create project" or "project template", I forget the exact name, but it’s on bitbucket or github… and "paster" from zopeskel seems to have potential to do something like this but it may be too zope specific, I don’t know.

    There are ways to do it, but IMO it’s something the framework should be handling. As it stands, "basic setup and configuration" is a much higher hurdle in Django than it is in Rails.

    > (also, FWIW I don’t have such a script — a new site at work always simply inherits default settings from other stuff, and those defaults are set up to match how our production servers work)

    Well, you don’t have a script that builds things out in a certain way, but you have a system in place that handles project defaults.

    This comment was originally posted on Hacker News

    24 djb_hackernews November 26, 2009 at 8:21 pm

    but Google thinks its MVT.http://www.google.com/search?q=django+mvt

    http://www.google.com/search?q=django+mtv

    This comment was originally posted on Hacker News

    26 tastybacon November 26, 2009 at 8:58 pm

    Has anyone seen a "Rails for a Django developer" out there?

    This comment was originally posted on Reddit

    30 mantaray November 27, 2009 at 1:56 am

    Just read that blog post from bottom to top…

    This comment was originally posted on Reddit

    38 sidmitra November 28, 2009 at 8:56 am

    Actually i would prefer it if models do most of the major work. And views to convert them into a format suitable for presentation. Sometimes that not possible, but it just feels cleaner and more manageable if it is.

    This comment was originally posted on Hacker News

    42 zkylab December 5, 2009 at 9:28 am

    very very good document / comparison.

    This comment was originally posted on Reddit

    Leave a Comment

    Additional comments powered by BackType

    Previous post:

    Next post: