mengu on web programming.

Django vs. web2py

Many people would like to know the differences between [Django](http://www.djangoproject.com) and [web2py](http://www.web2py.com). In this short entry, I will make a comparison of two popular Python web frameworks, Django and web2py. * Django is an [MTV framework](http://www.djangobook.com/en/1.0/chapter05/), web2py is an [MVC framework](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). * Web2py lets you focus on developing your application, Django doesn't. It wants you to do all its stuff. * With web2py you don't need to import anything but with Django you need to import everything that you will use. I think Django way is better here. However this is how web2py makes you focus on developing the application. * Web2py has no configuration or settings but Django has. Of course it is nothing compared to Java configuration files. * Both frameworks support backward compatibility. * Web2py automatically migrates your database, Django doesn't. Of course you can disable this if you wish. * Both frameworks support shell interacting. * Web2py automatically detects and renders the views but Django doesn't. * Web2py has two kinds of administration. One is Application Admin in where you can develop your application, the other one is Django like administration. Django has way more better administration panel than web2py with many options you can use. * Web2py has a better template system than Django. It is a lot permissive. No endif, endfor, endifequal, endifnotequal or {% %}. Everything goes between {{ }} and {{ pass }}. As of 1.2 [Django supports smart if tags](http://www.mengu.net/post/django-12-is-released). * Web2py lets you use Python expressions in your views but Django doesn't. In fact, Django's template system is limiting the developer. You can simply use {{ if a: }} show something {{ else: }} show another thing. {{ pass }} * Both frameworks support layouts. * Django has a way better ORM and creating models with Django is easier I think. While Django doesn't automatically import your models, you don't have to use file names such as 0_model.py, 1_model.py. This is not pretty. You can see some examples: # Django way: from django.db import models class Publisher(models.Model): name = models.CharField(maxlength=30) address = models.CharField(maxlength=50) city = models.CharField(maxlength=60) state_province = models.CharField(maxlength=30) country = models.CharField(maxlength=50) website = models.URLField() # web2py way: db.define_table('publisher', Field('name', length=30), Field('address', length=50), Field('city', length=60), Field('state_province', length=30), Field('country', length=50), Field('website', requires=IS_URL()) ) # Django way: p = Publisher(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/') p.save() # web2py way: p = db.publisher.insert(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/' ) * Django has a better URL routing than web2py. I find web2py routes more complex and difficult to set. Also both frameworks supports reverse url routing which is good. Django is a bit better on this since if you add a "name" attribute to the url, you can use it with the "url" template tag. * Both frameworks support internationalization however I find web2py's way a lot easier and elegant. * Web2py forms are more easy to use than Django forms. form1 = SQLFORM(db.publisher) # generates a create form. form2 = SQLFORM(db.publisher, db.post[1], deletable=True), # generates an update form. * Both frameworks support validation but Web2Py doesn't have object validation whereas Django has. (See [comment #6](http://www.mengu.net/post/django-vs-web2py#comment-6).) * Django templates support blocks and it supports it really good. Web2py's approach to template blocks was not the way it should be. As of 1.78.1 Web2Py supports blocks as Django. * Both frameworks support multiple databases. This is new in Django 1.2 release. * Both frameworks have great community. You can get help either on freenode channels or the google groups of both frameworks. This is all I can recall for now. I will update the post when necessary so do not hesitate to comment and contribute to comparison. Update (30/04/2010): Added #18. Update (11/05/2010): Django supports object validation. Update (18/05/2010): Updated #10. Updated #13. Updated #18. Added #19.
Did you enjoy this post? You should follow me on twitter here.

Comments

bsod1 said on 26/02/2010 23:30 PM
Any speed test about them?

Richard said on 27/02/2010 00:49 AM
Balanced overview. Regarding importing, I prefer the convenience of the web2py way but it mucks up the autocomplete in my IDE. One thing I find with Django is I am constantly referring to the docs while the web2py syntax sticks in my head easier.

Mengu Kagan said on 27/02/2010 02:07 AM
bsod1, there are not specific benchmarks comparing django and web2py however you can find many stand alone django and web2py benchmarks on google.

Mengu Kagan said on 27/02/2010 02:13 AM
Richard, Django documentation is the best documentation I have ever seen however as the web2py book finally became fully online web2py now has a great documentation too. :) My only concern regarding the importing is the speed of the application since it also imports the things that we won't use however as i have mentioned in the post, this is one of the ways how web2py makes us focus on development.

L said on 17/03/2010 12:31 PM
> Django has a better URL routing than web2py. I find web2py routes more complex and difficult to set. Also web2py supports reverse url routing which is good. I beg to differ. Django does reverse url. You mean like named views and reverse function or url tag in templates?

Mandx said on 10/05/2010 07:29 AM
Django now supports model validation (see docs) and models fields can have custom validators.

Mengu Kagan said on 11/05/2010 03:52 AM
L. and Mandx, Thank you for the information. I have updated the post.

coldhost said on 18/05/2010 02:18 AM
very good comparaion mengu. overall i feel web2py is more beautiful !

Mengu Kagan said on 19/05/2010 04:08 AM
Thanks coldhost, i'm glad you liked it. :)

Tomas said on 21/05/2010 03:51 AM
That's very helpful, thanks! For non-technical comparison, on indeed.com there are 6 web2py jobs and 660 Django jobs. (Not important if you are the one who decides what gets used.)

Mengu Kagan said on 21/05/2010 05:35 AM
Hi Tomas, Thank you for the information. That is actually the most the important thing over the technical details that one must consider if the goal is finding a job.

soberncrazy said on 11/08/2010 03:47 AM
Great post - I like Django so much I am afraid to try something new like web2py. But I probably will because nothing ventured is nothing gained.

ballyboy said on 05/09/2010 22:15 PM
And Tomas! there are thousands of PHP jobs... :-)

Richard said on 10/09/2010 08:34 AM
@soberncrazy: don't be afraid to try something new! Either your current opinion of Django will be reinforced or you will discover something better. I migrated from Django -> web2py 2 years back and will migrate again if something better comes along.

anish chapagain said on 22/09/2010 16:01 PM
hi, thank's for the comparison..yes Django really contributes but web2py is there to start learning python on web

Poul said on 22/09/2010 17:13 PM
web2py looks like Zope, little... I'm not sure but I see the same concept: TTW interface. After Zope I prefer another concept. Django looks more open, more "hackable"... PS. As for me I like CP :)

mdipierro said on 19/11/2010 21:38 PM
A few comments. Everything you can do with Django url.py you can do with web2py routes.py (both use regular expressions but there are some syntactical differences). Not sure the opposite is true (for example web2py routes.py can filter by client ip, virtual host and get/post). Both support reverse routing (it is used in all web2py official examples). web2py differs from Zope in two major ways: 1) the web based IDE (admin) is an app. If you do not use admin, web2py from the shell works like Django under the hood. Has a similar file stricture and no meta-data maintained by the web interface. 2) it uses relational databases and google app engine, not something weird (although good) like zodb.

juani said on 11/07/2011 23:34 PM
Here's what I read: the developers are so quick that any difference you can spot will be gone in weeks.

Mike A said on 17/08/2011 22:51 PM
I've used both frameworks on real projects. Both have many features. One big downside to web2py, the deal-breaker for me, is the global environment. It really makes things much harder, you can't tell where things came from and importing things isn't done normally. It screws up unit test runners, or any third party tool that needs to load code for that matter. This has caused us big headaches. I would go so far as to say that web2py is not real python code as it is so far from the standards, and it enforces its weird standards on you. It feels a lot like PHP.

Poul said on 18/08/2011 11:18 AM
Massimo, thank you for response. So, Web2py has TTW like Zope but is open (and more easier!) like Django. This very perspective and interesting framework needs to be "tried" :)

Mengu Kagan said on 18/08/2011 11:37 AM
@Mike A, I actually agree with you on there. It would be really good if we imported objects such as request, response, session, helpers and many other things explicitly. web2py makes everything available and accessible globally so the objects that we are not going to use are also available which is no good. however as i have mentioned above, this is how web2py makes us productive. we are not dealing with boilerplate. :)

Pedro said on 18/09/2011 04:32 AM
Good comparison, nice that you provide some sample code to illustrate the differences. Just one thing I find a bit misleading, the validation. I don't know why is relevant to mention that web2py doesn't have a validation object. Web2py validations typically live in the forms, which makes more sense IMHO. I using django for the first time and so far I'm enjoying it. The documentation can't be compared to its state during django first years. I've tred to learn django before a few years ago and the documentation was very poor and incomplete. For those trying to choose between the two, some more [hopefully] relevant info: - django needs more boilerplate code. If you've used web2py this may feel a bit unnecessary, but to be honest, it's not a big deal since it's well documented. - web2py has a batteries included philosophy. It works out of the box it does all the tedious things for you so you can focus on development. It's a deadly precision weapon if you need quick turn around. Database migrations, forms with built in validation, an very useful issue tracking system, zero install, etc. - django tries to encourage a modular architecture, multiple applications are developed for several roles but they can work together. I like the concept. Having been working on environments where projects often overlap, this is a smart approach that makes code reuse a rather simple task. If I had to choose a framework for a project,that would be web2py, it's simply my favourite framework. But, if we're talking about projects with large code bases and many people involved, django might be more suited for that. Not such a technical aspect, but still relevant: Besides being a very skilled programmer, Massimo (web2py author) is a nice and very helpful guy.

Mengu Kagan said on 19/09/2011 02:02 AM
hello pedro, thanks for the feedback. massimo, in my opinion, is probably the only guy who cares about the community's opinions and requests in the python web framework world. he also improves himself a lot and always open to new and crazy ideas. the only criticism i have against him is that he cares too much about backward compatibility. it is bad. it is preventing, it is limiting. i hope things would change in web2py 2.0.

Sefa Denizoglu said on 17/11/2011 03:16 AM
Hi mengu and all, @mengu thank you for the review first of all, I've been trying to figure out some web framework just to let me learn/exercise on some web developing skills WITH a "need to learn python" focus. First I've (heard/got to know) Django, after fiddling with it for a couple days, reading a quite more views on what the "Django-way" provides people and finally find out about web2py, I think I'll stick to web2py. The tipping point for me was pedro's comment; "If I had to choose a framework for a project,that would be web2py, but, if we're talking about projects with large code bases and many people involved, django might be more suited for that." Just needed to thank you all for sharing invaluable comments and views of yours.

Mick said on 18/01/2012 18:08 PM
great article, I'm looking for comparison of web2py nad flask, do you know where to find that?

Freerk said on 22/05/2012 21:44 PM
Thanks for sharing your insights. Coming from PHP I am starting to develop in Python. Tried Django for a few days, but am not overenthousiastic about it. Web2py will be the next framework to play with.

Joe said on 03/07/2012 12:02 PM
Very nice article! Thanks for this valuable information. Please, please keep it updated (if possible; I guess it's a bit tough to keep it up to date since these frameworks evolve very fast!) :-) Again, thx!

Thinlinedata said on 04/09/2012 00:54 AM
Which of the languages support the most protocols like JSON, XML etc? Is one more than the other? Also since web2py allows pure Python code to be written, should that not be the deal breaker here?

CC Roy said on 11/10/2012 11:41 AM
Which one of the two has better Authentication System : Log-in, Registration, Access Controls & Restrictions, Session Management, etc.? Also, can you please post some statistics on final applications performance, memory use, size, number of files, execution time for specific tasks which are repeatedly used, etc. Thank you, very much.

Leave a Response

No HTML allowed. You can use markdown.
Name*:
E-Mail* (not published):
Web site:
Response: