DJANGO VS. WEB2PY

Many people would like to know the differences between Django and web2py. In this short entry, I will make a comparison of two popular Python web frameworks, Django and web2py.

  • Django is an MTV framework, web2py is an MVC framework.

  • 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.

  • 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

     
    1. {{ if a: }}

    2. show something

    3. {{ else: }}

    4. show another thing.

    5. {{ pass }}

    6.  
  • 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:

     
    1. # Django way:

    2. from django.db import models

    3. class Publisher(models.Model):

    4. name = models.CharField(maxlength=30)

    5. address = models.CharField(maxlength=50)

    6. city = models.CharField(maxlength=60)

    7. state_province = models.CharField(maxlength=30)

    8. country = models.CharField(maxlength=50)

    9. website = models.URLField()

    10.  
    11. # web2py way:

    12. db.define_table('publisher',

    13. Field('name', length=30),

    14. Field('address', length=50),

    15. Field('city', length=60),

    16. Field('state_province', length=30),

    17. Field('country', length=50),

    18. Field('website', requires=IS_URL())

    19. )

    20.  
    21. # Django way:

    22. p = Publisher(name='Apress',

    23. address='2855 Telegraph Ave.',

    24. city='Berkeley',

    25. state_province='CA',

    26. country='U.S.A.',

    27. website='http://www.apress.com/')

    28. p.save()

    29.  
    30. # web2py way:

    31. p = db.publisher.insert(name='Apress',

    32. address='2855 Telegraph Ave.',

    33. city='Berkeley',

    34. state_province='CA',

    35. country='U.S.A.',

    36. website='http://www.apress.com/'

    37. )

    38.  
  • 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.

     
    1. form1 = SQLFORM(db.publisher) # generates a create form.

    2. form2 = SQLFORM(db.publisher, db.post[1], deletable=True), # generates an update form.

    3.  
  • Both frameworks support validation but Web2Py doesn't have object validation whereas Django has. (See 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.

--------------------- 本文来自 snail200x 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/snail200x/article/details/9612283?utm_source=copy

猜你喜欢

转载自blog.csdn.net/weiqubo/article/details/82918201